How can I make grep print the lines below and above each matching line?

grep, linux

Solution

grep's `-A 1` option will give you one line after; `-B 1` will give you one line before; and `-C 1` combines both to give you one line both before and after, `-1` does the same.

Problem

I want to search each line for the word `FAILED`, then print the line above and below each matching line, as well as the matching line. Input: ``` id : 15 Status : SUCCESS Message : no problem id : 15 Status : FAILED Message : connection error ``` Desired output for `grep 'FAILED'`: ``` id : 15 Status : FAILED Message : connection error ```

Original source

Related problems