转自:http://blog.csdn.net/stormkey/article/details/5905204
版权声明 :转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://gosman.blogbus.com/logs/8150093.html
作者:mysurface
译者:gosman(lianmingchang2008#gmail.com)
版本:V 1.0.0
时间:2007-9-7
这是个小技巧,grep的A(after,后)选项和B(before,前)选项可以同时输出其匹配行的前后几行。
比如包含有如下文本的message.txt:
Aug 5 02:43:12 zion kernel: [ 0.000000] Zone PFN ranges: Aug 5 02:43:12 zion kernel: [ 0.000000] DMA 0 -> 4096 Aug 5 02:43:12 zion kernel: [ 0.000000] Normal 4096 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] HighMem 130730 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] early_node_map[1] active PFN ranges Aug 5 02:43:12 zion kernel: [ 0.000000] 0: 0 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] DMI 2.3 present.
用带-B1和-A2选项的grep匹配搜索"DMA"。
grep -B1 -A2 "DMA" message.txt
输出:
Aug 5 02:43:12 zion kernel: [ 0.000000] Zone PFN ranges: Aug 5 02:43:12 zion kernel: [ 0.000000] DMA 0 -> 4096 Aug 5 02:43:12 zion kernel: [ 0.000000] Normal 4096 -> 130730 Aug 5 02:43:12 zion kernel: [ 0.000000] HighMem 130730 -> 130730
grep匹配一个结果,输出多行的功能,在搜索日志文件时很有用。