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

Issue

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

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.

Answered By – pgs

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published