Perl在文字字符上拆分字符串\n



我有一个来自上游系统的文件。它包含一个非常大的字符串,并且包含一个文本n。我需要在文本n上拆分那个大字符串。

这是我正在处理的文件:

onensometingntwonthreenmore thingsnsome more things

如上所述,我需要在文本n上拆分这个大字符串,预期输出如下:

one
something
two
three
more things
some more things

如果字符串包含两个字符+n而不是换行符,则需要"逃逸;使用split时为,因此变为\

示例:

#!/bin/perl
my $str='onensometingntwonthreenmore thingsnsome more things';
my @splitted = split/\n/, $str;    # split on the two characters '' + 'n'
print join("n", @splitted) . "n";

您可以像使用其他任何东西一样使用split

@x="一件事两件事三件事更多";

@split_x=split/\n/,$x;

然后可以以拆分的方式打印:

打印联接"\n〃@split_x;

或甚至作为一行

打印联接"\n〃;,拆分/\n/,$x;

最新更新