How do I grep
and show the preceding and following 5 lines surrounding each matched line?
13 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.
-
60It is good but unfortunately the Solaris grep does not support that. See that link for solaris: unix.com/solaris/33533-grep-display-few-lines-before-after.html– рüффпMar 21, 2011 at 12:55
-
9Ok, but what if want to show all lines of output after the match? grep -A0 and grep -A-1 don't cut it...– user67416Jul 22, 2011 at 2:18
-
2does not work for me for some reason, although mentioned in my man pages. Aug 1, 2012 at 9:43
-
2If you are HP-UX env, none of the grep versions will work like in Solaris. Was able to use the Solaris link but replace nawk with awk in that link.– zkarthikJan 23, 2013 at 21:45
-
6-n is for line numbers, but for some versions of grep -n# will show # surrounding lines (like -c) with line numbers. That's a useful shortcut that's my go-to when I need context. May 7, 2013 at 16:24
-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).
-
3I tried the
-n
format and found out it only works till9
. For15
it returns 5 lines Mar 27, 2019 at 7:49 -
20@DeepakMahakale This is probably related to how command-line arguments / options are typically parsed by POSIX programs. The option specifier is a single character (such as
-A
,-B
or-C
). Usually, the option specifier is followed by a value (-o a.out
to specify output file in GCC), but it can also function as a simple switch / flag (-g
to enable debugging info in GCC). However spaces between options are optional, so for options without a value, it is possible to merge them (-ABC
), which means that-15
is interpreted-1 -5
(two separate options) and the-5
overrides the-1
.– natiiixApr 19, 2019 at 12:17 -
2-5 is quicker than both -A 5 -B 5. Those are not meant to be used together. It is cleaner to other readers of the script if you choose -A or -B or -C over -9. Jun 7, 2019 at 17:01
-
1
grep astring myfile -A 5 -B 5
That will grep "myfile" for "astring", and show 5 lines before and after each match
-
25
-
4
ripgrep
If you care about the performance, use ripgrep
which has similar syntax to grep
, e.g.
rg -C5 "pattern" .
-C
,--context NUM
- Show NUM lines before and after each match.
There are also parameters such as -A
/--after-context
and -B
/--before-context
.
The tool is built on top of Rust's regex engine which makes it very efficient on the large data.
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
-
1Did you not read the accepted answer? You are just repeating what has already been said on a question almost 10 years old...– YokaiJan 13, 2018 at 9:22
-
9Oh I'm sorry Yokai. But I don't read anything about grepping the help section of grep to retrieve the answer.– chtenbJan 13, 2018 at 10:30
-
1@Yokai Besides what Chiel ten Brinke said, the accepted answer does not mention the long options Sep 13, 2019 at 19:57
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
Trick to remember options:
-A
→ A means "After"-B
→ B means "Before"-C
→ C means "in between"
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
-
2On what system (incl. distribution name and version) did you try it? Was it installed by default? If not, what are some installation instructions? It is not installed by default on Ubuntu MATE 20.04 (Focal Fossa). Jul 17, 2021 at 22:05
Search for "17655" in /some/file.txt
showing 10 lines context before and after (using Awk
), output preceded with line number followed by a colon. Use this on Solaris when grep
does not support the -[ACB]
options.
awk '
/17655/ {
for (i = (b + 1) % 10; i != b; i = (i + 1) % 10) {
print before[i]
}
print (NR ":" ($0))
a = 10
}
a-- > 0 {
print (NR ":" ($0))
}
{
before[b] = (NR ":" ($0))
b = (b + 1) % 10
}' /some/file.txt;
I do it the compact way:
grep -5 string file
That is the equivalent of:
grep -A 5 -B 5 string file
Here is the @Ygor solution in awk
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=3 s="pattern" myfile
Note: Replace a
and b
variables with number of lines before and after.
It's especially useful for system which doesn't support grep's -A
, -B
and -C
parameters.
-
1Very nice. Yes, it is more complex-looking, but it is actually cross-platform. Tested on AIX and HP-UX. Nov 8, 2021 at 13:55
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
.
man grep | grep -C 1 context
:)man grep | grep -C 1 "\-C"
;)