tags:

views:

569

answers:

3

Hi,

I have to parse a very large file and I want to use the command grep (or any other tool).

I have to check in a log line the word "FAILED", print the line above and below.

For example:

id : 15
Satus : SUCESS
Message : no problem


id : 15
Satus : FAILED
Message : connection error


And I need to print only

id : 15
Satus : FAILED
Message : connection error

Thank you for your help !

+2  A: 

Use -A and -B switches (mean lines-after and lines-before):

grep -A 1 -B 1 FAILED file.txt
Milan Babuškov
+6  A: 

Use -B and -A option

grep --help
...
-B, --before-context=NUM  print NUM lines of leading context
-A, --after-context=NUM   print NUM lines of trailing context
...
zdmytriv
+6  A: 

grep's -A 1 option will give you one life after; -B 1 will give you one line before; and -C 1 combines both to give you one line both before and after.

pgs
+1, I forgot about -C (lines of context)
Milan Babuškov