现在的位置: 首页 > 综合 > 正文

perl实现多行模式匹配

2017年12月15日 ⁄ 综合 ⁄ 共 490字 ⁄ 字号 评论关闭

因为grep不能匹配换行符grep只能单行匹配字符串,但是可以通过-A等选项打印上下文环境;sed在读入一行的时候会自动去掉末尾的换行,所以sed在进行多行匹配的时候很复杂。这里使用perl,先把文件的全部内容读入,然后进行多行匹配。代码如下:


my $log_name = $ARGV[0];
open(FILE,"$log_name") or die "can't open $file $!\n";
my $content = join("",<FILE>);
while($content =~ /<data>.*<\/data>\n<user>hongchangfirst<\/user>\n<msg><!\[ZHC\]><\/msg>\n/g)
{
    print $&,"\n";
}
close(FILE);

这就可以把所有匹配的行都打印出来。

原文:http://blog.csdn.net/hongchangfirst/article/details/25044681

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst

抱歉!评论已关闭.