grep: show lines surrounding each match

Viewed 1349539

How do I grep and show the preceding and following 5 lines surrounding each matched line?

15 Answers

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

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

grep -C 3 foo README.txt

This will show 3 lines before and 3 lines after.

-A and -B will work, as will -C n (for n lines of context), or just -n (for n lines of context... as long as n is 1 to 9).

ack works with similar arguments as grep, and accepts -C. But it's usually better for searching through code.

grep astring myfile -A 5 -B 5

That will grep "myfile" for "astring", and show 5 lines before and after each match

I normally use

grep searchstring file -C n # n for number of lines of context up and down

Many of the tools like grep also have really great man files too. I find myself referring to grep's man page a lot because there is so much you can do with it.

man grep

Many GNU tools also have an info page that may have more useful information in addition to the man page.

info grep

Use grep

$ grep --help | grep -i context
Context control:
  -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

If you search code often, AG the silver searcher is much more efficient (ie faster) than grep.

You show context lines by using the -C option.

Eg:

ag -C 3 "foo" myFile

line 1
line 2
line 3
line that has "foo"
line 5
line 6
line 7

You can use option -A (after) and -B (before) in your grep command.

Try grep -nri -A 5 -B 5 .

I do it the compact way:

grep -5 string file

That is the equivalent of:

grep -A 5 -B 5 string file

Grep has an option called Context Line Control, you can use the --context in that, simply,

| grep -C 5

or

| grep -5

Should do the trick

$ grep thestring thefile -5

-5 gets you 5 lines above and below the match 'thestring' is equivalent to -C 5 or -A 5 -B 5.

Let's understand using an example.
We can use grep with options:

-A 5  # this will give you 5 lines after searched string.
-B 5  # this will give you 5 lines before searched string.
-C 5  # this will give you 5 lines before & after searched string

Example. File.txt contains 6 lines and following are the operations.

[abc@xyz]~/% cat file.txt # print all file data
this is first line
this is 2nd line
this is 3rd line
this is 4th line
this is 5th line
this is 6th line

[abc@xyz]~% grep "3rd" file.txt # we are searching for keyword '3rd' in the file
this is 3rd line

[abc@xyz]~% grep -A 2 "3rd" file.txt # print 2 lines after finding the searched string
this is 3rd line
this is 4th line
this is 5th line

[abc@xyz]~% grep -B 2 "3rd" file.txt # Print 2 lines before the search string.
this is first line
this is 2nd line
this is 3rd line

[abc@xyz]~% grep -C 2 "3rd" file.txt # print 2 line before and 2 line after the searched string 
this is first line
this is 2nd line
this is 3rd line
this is 4th line
this is 5th line
Related