regexgreppcregrep

Regex for multiline patterns then they are following each other


I'm trying to find lines that have "aaa" and "bbb" in that order, and those strings are on different lines.

11 aaa blah blah
bbb blah blah
blah blah blah blah
aaa blah blah blah blah
blah blah blah blah
11 bbb blah blah
blah blah blah blah

With this multiline pattern .*?(.|\n)*? I get all the lines between my strings:

11 aaa blah blah
bbb blah blah

aaa blah blah blah blah
blah blah blah blah
11 bbb blah blah

But I need to match them only then they are following each other. E.g. aaa on the 1st line, bbb on the 2nd. The output should be:

11 aaa blah blah
bbb blah blah

Solution

  • You can use

    pcregrep -Mo '.*aaa.*\R.*bb.*' file
    

    The M option allows pcregrep to "see" line breaks and match them with the regex and o will output the texts matched.

    The pattern matches