我想在每行中搜索单词FAILED,然后打印每个匹配行上下的行,以及匹配行。
FAILED
输入:
id : 15 Status : SUCCESS Message : no problem id : 15 Status : FAILED Message : connection error
grep 'FAILED'的期望输出:
grep 'FAILED'
id : 15 Status : FAILED Message : connection error
使用-A和-B开关(前后平均行):
grep -A 1 -B 1 FAILED file.txt
使用-B, -A或-C选项
grep --help ... -B, --before-context=NUM print NUM lines of leading context -A, --after-context=NUM print NUM lines of trailing context -C, --context=NUM print NUM lines of output context -NUM same as --context=NUM ...
grep的-A 1选项将在;-B 1将在;而-C 1将两者结合在一起,在前后都给出一行,-1也是这样做的。
-A 1
-B 1
-C 1
-1