Skip to main content
1 of 4
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Using any awk in any shell on every Unix boox:

$ awk -v ORS='---separator---\n' '/^Compiling/{buf=""} {buf=buf $0 "\n"} /^error:/{print buf}' file Compiling File3 ... commands ... In file included from ... In file included from ... In file included from ... error: could not find A ---separator--- Compiling File5 ... commands ... In file included from ... In file included from ... In file included from ... error: could not find B ---separator--- 

Alternatively, using GNU awk for multi-char RS and RT:

$ awk -v RS='\nerror:[^\n]+' -v ORS='\n---separator---\n' ' sub(/(^|.*\n)Compiling/,"Compiling") { print $0 RT } ' file Compiling File3 ... commands ... In file included from ... In file included from ... In file included from ... error: could not find A ---separator--- Compiling File5 ... commands ... In file included from ... In file included from ... In file included from ... error: could not find B ---separator--- 
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60