grep in a textfile all lines containing 'xxx' and the previous line

catalina.out, grep

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

Problem

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?

Original source