Search code examples
regexsedgrepcygwin

Multiple line, repeated occurence matching


I refer to below question, but with a bit difference. I need to only get line that has "abc" when there is "efg" matching at different line. And I only need the latest matched "abc" line before "efg" is matched...

How to find patterns across multiple lines using grep?

blah blah..
blah blah..
blah abc blah1
blah blah..
blah blah..
blah abc blah2
blah blah..
blah efg1 blah blah
blah efg2 blah blah
blah blah..
blah blah..

blah abc blah3
blah blah..
blah blah..
blah abc blah4
blah blah..
blah blah blah

blah abc blah5
blah blah..
blah blah..
blah abc blah6
blah blah..
blah efg3 blah blah

blah efg4 blah blah
blah abc blah7
blah blah..
blah blah..
blah abc blah8
blah blah..

Expected output

blah abc blah2
blah abc blah6

Solution

  • This might work for you (GNU sed):

    sed -n '/abc/h;/efg/!b;x;/abc/p;z;x' file
    

    Store the latest abc line in the hold space (HS). When encountering a line containing efg, switch to the HS and if that line contains abc print it.