Skip to main content
5 of 5
more elaborate explanation
mik
  • 1.4k
  • 11
  • 15

If you have awk you can do:

awk '/foo/{print b$0;b="";x=1;next} x{b=b$0"\n"}' bar.log | less

When a foo appears, it prints buffer (b variable) and current line, and clears the buffer.

Otherwise, but only if foo already appeared (x variable) it buffers current line.

mik
  • 1.4k
  • 11
  • 15