grep a file, but show several surrounding lines?

The explanation for grep a file

Here we will grep a file, but we will also show the several surrounding lines

So in the 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

And if you are willing to have the same number of lines before and after. At that time you can use -C num.

grep -C 3 foo README.txt

And by using this will show the 3 lines. Which are 3 lines before and 3 lines after. 

 

Also read, Why are these datagrams reassembled to a single datagram

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *