如何使用grep跨多行查找模式? [英] How to find patterns across multiple lines using grep?

查看:131
本文介绍了如何使用grep跨多行查找模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照该顺序查找具有abc和efg的文件,并且这两个字符串在该文件中位于不同的行上。例如:一个包含内容的文件:

 等等等等.. 
等等等等..
等等等等等等等等等
等等等等..
等等等等..
等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等等$ b

匹配。 / div>

Grep对于此操作是不够的。



pcregrep 在大多数现代Linux系统中都可以用作

  pcregrep -M'abc。*(\\\
|。)* efg'test.txt

还有一个更新的 pcre2grep 。两者都由 PCRE项目提供。



pcre2grep可用通过 Mac Ports 作为端口 pcre2 :

 %sudo port install pcre2 

以及通过自制

 %brew install pcre 


I want to find files that have "abc" AND "efg" in that order, and those two strings are on different lines in that file. Eg: a file with content:

blah blah..
blah blah..
blah abc blah
blah blah..
blah blah..
blah blah..
blah efg blah blah
blah blah..
blah blah..

Should be matched.

解决方案

Grep is not sufficient for this operation.

pcregrep which is found in most of the modern Linux systems can be used as

pcregrep -M  'abc.*(\n|.)*efg' test.txt

There is a newer pcre2grep also. Both are provided by the PCRE project.

pcre2grep is available for Mac OS X via Mac Ports as part of port pcre2:

% sudo port install pcre2 

and via Homebrew as:

% brew install pcre

这篇关于如何使用grep跨多行查找模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆