How to find patterns across multiple lines using grep?

后端 未结 26 2067
你的背包
你的背包 2020-11-22 04:14

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 bl         


        
26条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 04:39

    If you have some estimation about the distance between the 2 strings 'abc' and 'efg' you are looking for, you might use:

    grep -r . -e 'abc' -A num1 -B num2 | grep 'efg'
    

    That way, the first grep will return the line with the 'abc' plus #num1 lines after it, and #num2 lines after it, and the second grep will sift through all of those to get the 'efg'. Then you'll know at which files they appear together.

提交回复
热议问题