I want to print all lines in a tomcat catalina.out log containing xxx. A simple thing to accomplish using:
cat catalina.out | grep xxx
However. In the logfile I get the lines containing xxx, the line above this line is containing the date and time when the item was logged. I would like to see those lines above the grepped lines too. How could I accomplish this?
grep -B1-B[n]lets you see[n]lines before the pattern that you are looking for.You can also use
-Afor 'lines after', and-Cfor 'context' (lines both above and below).You can also simplify your grep call and remove the pipe with
grep xxx -B1 catalina.out.