Question

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?

Was it helpful?

Solution

grep -B1

-B[n] lets you see [n] lines before the pattern that you are looking for.

You can also use -A for 'lines after', and -C for 'context' (lines both above and below).

You can also simplify your grep call and remove the pipe with grep xxx -B1 catalina.out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top