Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

search - grep一个文件,但显示几个周围的行?(grep a file, but show several surrounding lines?)

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.

(我想grep一个字符串,但也显示前五行和以下五行以及匹配的行。)

How would I be able to do this?

(我怎么能这样做?)

  ask by Mark Harrison translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(对于BSDGNU grep您可以使用-B num来设置匹配前的行数和匹配后的行数的-A num 。)

grep -B 3 -A 2 foo README.txt

If you want the same number of lines before and after you can use -C num .

(如果你想要使用-C num之前和之后的相同行数。)

grep -C 3 foo README.txt

This will show 3 lines before and 3 lines after.

(这将显示之前的3行和之后的3行。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...