grep 多行 正则匹配

https://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep

 

I relied heavily on pcregrep,but with newer grep you do not need to install pcregrep for many of its features. Just use grep -P.

In the example of the OP‘s question,I think the following options work nicely,with the second best matching how I understand the question:

grep -Pzo "abc(.|\n)*efg" /tmp/tes* grep -Pzl "abc(.|\n)*efg" /tmp/tes*

I copied the text as /tmp/test1 and deleted the ‘g‘ and saved as /tmp/test2. Here is the output showing that the first shows the matched string and the second shows only the filename (typical -o is to show match and typical -l is to show only filename). Note that the ‘z‘ is necessary for multiline and the ‘(.|\n)‘ means to match either ‘anything other than newline‘ or ‘newline‘ - i.e. anything:

[email protected]:~$ grep -Pzo "abc(.|\n)*efg" /tmp/tes* /tmp/test1:abc blah blah blah.. blah blah.. blah blah.. blah efg [email protected]:~$ grep -Pzl "abc(.|\n)*efg" /tmp/tes* /tmp/test1

To determine if your version is new enough,run man grep and see if something similar to this appears near the top:

 -P, --perl-regexp Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and grep -P may warn of unimplemented features.

That is from GNU grep 2.10.

相关文章

jquery.validate使用攻略(表单校验) 目录 jquery.validate...
/\s+/g和/\s/g的区别 正则表达式/\s+/g...
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母...
this.optional(element)的用法 this.optional(element)是jqu...
jQuery.validate 表单动态验证 实际上jQuery.validate提供了...
自定义验证之这能输入数字(包括小数 负数 ) <script ...