grep 一个文件,但显示几个周围的行? [英] grep a file, but show several surrounding lines?
本文介绍了grep 一个文件,但显示几个周围的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对一个字符串进行grep
,但还要显示前五行和后五行以及匹配的行.我如何才能做到这一点?
I would like to grep
for a string, but also show the preceding five lines and the following five lines as well as the matched line. How would I be able to do this?
推荐答案
对于 BSD 或 GNU grep
你可以使用 -B num
设置匹配前的行数,-A num
设置匹配后的行数.
For BSD or GNU grep
you can use -B num
to set how many lines before the match and -A num
for the number of lines after the match.
grep -B 3 -A 2 foo README.txt
如果希望前后行数相同,可以使用-C num
.
If you want the same number of lines before and after you can use -C num
.
grep -C 3 foo README.txt
这将显示前 3 行和后 3 行.
This will show 3 lines before and 3 lines after.
这篇关于grep 一个文件,但显示几个周围的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文