How to find patterns across multiple lines using grep?

后端 未结 26 1727
你的背包
你的背包 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:19

    I released a grep alternative a few days ago that does support this directly, either via multiline matching or using conditions - hopefully it is useful for some people searching here. This is what the commands for the example would look like:

    Multiline:

    sift -lm 'abc.*efg' testfile
    

    Conditions:

    sift -l 'abc' testfile --followed-by 'efg'
    

    You could also specify that 'efg' has to follow 'abc' within a certain number of lines:

    sift -l 'abc' testfile --followed-within 5:'efg'
    

    You can find more information on sift-tool.org.

提交回复
热议问题