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 -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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top