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:

user@host:~$ grep -Pzo "abc(.|\n)*efg" /tmp/tes*
/tmp/test1:abc blah
blah blah..
blah blah..
blah blah..
blah efg
user@host:~$ 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.

grep 多行 正则匹配的更多相关文章

  1. grep[行号&正则匹配字符有颜色]

    事情是这样的,昨天在深入学习grep命令时,看到别人博客用grep正则匹配,不仅行数有颜色,而且匹配到的字符也有颜色.我在CRT也试了下,毛颜色都没有.顿时感觉 so low. 解决 编辑vim~/. ...

  2. Linux篇---Grep和正则匹配

    一.前述 Linux中正则匹配查找比较常用,所以分享一篇关于正则匹配和Grep结合的文章. 二.匹配规则 匹配操作符: \                  转义字符.                 ...

  3. liux三剑客grep 正则匹配

    001正则匹配(大部分需要转义) ‘^‘: 锚定行首 '$' : 锚定行尾 [0-9] 一个数字 [^0-9] 除去数字所有,^出现在[]这里表示取反 [a-z] [A-Z] [a-Z] \s 匹配空 ...

  4. grep中正则匹配的使用

    如要匹配Computer或computer两个单词,可做如下操作: [Cc]mputer “.”允许匹配ASCII集中任意字符,或为字母,或为数字. 使用\{\}匹配模式结果出现的次数 匹配字母A出现 ...

  5. Shell遍历文件,对每行进行正则匹配

    Shell查看文件的最后5行,并对每行进行正则匹配,代码如下: #!/bin/sh pattern="HeartBeat" /home/test/log/log_20150205. ...

  6. phpstorm 正则匹配删除注释行(替换注释行为空行)

    使用phpstorm 来编写php 和javascript 代码,感觉还是不错的,用得也很舒服. 遇到了一个需求,有时候在阅读框架源代码的时候 , 想过滤(删除)掉源代码中的注释行,如果手动逐行删除显 ...

  7. grep 正则匹配

    \{0,n\}:至多n次 \{\ 匹配/etc/passwd文件中数字出现只是数字1次到3次 匹配/etc/grub2.cfg文件以一个空格开头匹配一个字符的文件的所有行 显示以LISTEN结尾的行 ...

  8. grep 常用正则匹配

    1.或操作 grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行 egrep '123|abc' filename // 用egrep ...

  9. shell grep正则匹配汉字

    Shell grep正则匹配中文 测试文本 demo_exe.c,内容如下,需要注意保存的编码格式,对输出到终端有影响: 我们中文操作系统ASNI默认是GBK的. #include<stdio. ...

随机推荐

  1. python 全栈开发,Day129(玩具开机提示语,为多个玩具发送点播,聊天界面,app录音,app与服务器端文件传输,简单的对话)

    一.玩具开机提示语 先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.2.zi ...

  2. python 全栈开发,Day62(外键的变种(三种关系),数据的增删改,单表查询,多表查询)

    一.外键的变种(三种关系) 本节重点: 如何找出两张表之间的关系 表的三种关系 一.介绍 因为有foreign key的约束,使得两张表形成了三种了关系: 多对一 多对多 一对一 二.重点理解如果找出 ...

  3. JQuery中的Ajax(六)

    一:Ajax请求jQuery.ajax(options) load(url,[data],[callback])jQuery.get(url,[data],[callback]) jQuery.get ...

  4. C#检查服务状态和启动关闭服务

    WinForm 判断服务状态,显示服务名称和状态 https://blog.csdn.net/u013063880/article/details/78626200 C#获得服务,判断服务状态,启动服 ...

  5. [转]使用tee记录mysql client 所有的操作,十个节省时间的mysql命令

    查看:tail -f /tmp/jack.txt 一:tee方法一.配置文件在服务器上的/etc/my.cnf中的[client]加入 tee =/tmp/client_mysql.log即可. 方法 ...

  6. CentOS6.8安装360 pika

    1.安装依赖包 yum install snappy-devel bz2 libzip-dev libsnappy-dev libprotobuf-dev libevent-dev protobuf- ...

  7. 使用ssh命令进行远程登录

    1.查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号.使用ssh -V命令可以得到版本号.需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用Op ...

  8. JDK1.7+Tomcat6.0+MyEclipse8.6在win7下的安装与配置

    http://wenku.baidu.com/view/4f0bef02192e45361066f548.html

  9. mydumper下载安装

    下载地址   https://github.com/maxbube/mydumper [root@gg ~]#cd mydumper [root@gg mydumper]# cmake . -bash ...

  10. Unity 之 rawimage 与image 的区别

    http://www.newbieol.com/information/740.html