如何搜索模式从一行开始到另一行结束?

时间:2019-01-28 10:33:10

标签: linux

我正在搜索文件中从第3行开始到第4行结束的模式,如何在linux中搜索这些模式

有文本的猫myfile.txt

you know who is super hero  
where they are  
super hero are born by birth,  
they are good for us.

grep -irls'超级英雄是天生的,对我们有好处'*

2 个答案:

答案 0 :(得分:1)

如果pcregrep适用于您的系统,则易于使用:

pcregrep -M -irls 'super hero are born by birth,\nthey are good for us' *

-M启用多行匹配。 \n匹配换行符。

如果报价在任何时候都可能被分成多行,那么您可以将(恰好匹配一个空格)的每次出现都替换为\s+(匹配任何一个或多个匹配项) “空白”字符):

pcregrep -M -irls 'super\s+hero\s+are\s+born\s+by\s+birth,\s+they\s+are\s+good\s+for\s+us' *

答案 1 :(得分:0)

如果您有GNU grep,请尝试以下操作:

grep -zPirls 'super hero are born by birth,[[:space:]]*they are good for us' *

GNU awk:

awk -v RS='' '/super hero are born by birth,[[:space:]]*they are good for us/{print FILENAME}' *