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

Possible Duplicate:
grep a file, but show several surrounding lines?

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

I want to search each log line for the word FAILED , then print the line above and below each matching line, as well as the matching line.

For example:

id : 15
Satus : SUCCESS
Message : no problem

id : 15
Satus : FAILED
Message : connection error

And I need to print:

id : 15
Satus : FAILED
Message : connection error

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.


使用-B和-A选项

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

使用-A和-B开关(之前和之后的平均线):

grep -A 1 -B 1 FAILED file.txt
链接地址: http://www.djcxy.com/p/3310.html

上一篇: 如何递归地grep?

下一篇: 我怎样才能让grep打印每条匹配行下面和上面的行?