How to find patterns across multiple lines using grep?

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

    Why not something simple like:

    egrep -o 'abc|efg' $file | grep -A1 abc | grep efg | wc -l
    

    returns 0 or a positive integer.

    egrep -o (Only shows matches, trick: multiple matches on the same line produce multi-line output as if they are on different lines)

    • grep -A1 abc (print abc and the line after it)

    • grep efg | wc -l (0-n count of efg lines found after abc on the same or following lines, result can be used in an 'if")

    • grep can be changed to egrep etc. if pattern matching is needed

提交回复
热议问题