所以我试图从日志行中删除嵌入的\ n而不从命令行中删除每个日志行的\ n。 我已经试过这些,他们都改变了所有\ n〜。Perl的正则表达式脚本和命令行不同
cat test1.txt | perl -n -e 's{\n(?!2013)}{~}mg;print' > test1a.fix
perl -n -e 's{\n(?!2013)}{~}mg;print' test1.txt > test1b.fix
所有忽视的负面看后面。
test1.txt的包含
2013-03-01 12:23:59,1
line2
line3
2013-03-01 12:23:59,4
test1a.fix和test1b.fix包含
2013-03-01 12:23:59,1~line2~ line3~2013-03-01 12:23:59,4
但我想出了使用该脚本的正则表达式。
#!/usr/bin/perl
use warnings;
use strict;
sub test {
my ($str, $expect) = @_;
my $mod = $str;
$mod =~ s{\n(?!2013)}{~}mg;
print "Expecting '$expect' got '$mod' - ";
print $mod eq $expect ? "passed\n" : "failed\n";
}
test("2013-03-01 12:23:59,line1
line2
line3
2013-03-01 12:23:59,line4", "2013-03-01 12:23:59,line1~line2~ line3
2013-03-01 12:23:59,line4");
它会产生以下与我想要的匹配的输出。
[email protected]:~/logs> ./regex_test.pl
Expecting '2013-03-01 12:23:59,line1~line2~ line3
2013-03-01 12:23:59,line4' got '2013-03-01 12:23:59,line1~line2~ line3
2013-03-01 12:23:59,line4' - passed
[email protected]:~/logs>
任何人都可以解释为什么这些工作不同以及如何可以在命令行上做到这一点?
感谢搜索命令行perl的多行选项,并找到-0777标志,并使其正常工作。 perl -0777 -n -e's {\ n(?!2013)} {〜} mg; print'test1.txt> test1c.fix 谢谢所有 –
我在这里学到了一些东西。 (来自perl --help:-0 [octal]指定记录分隔符) – ddoxey