Categories
command-line-interface grep logging search

grep: show lines surrounding each match

3949

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

6

  • 3

    I keep a copy of Brendan Gregg’s perl script around for this purpose. Works well.

    Sep 4, 2008 at 19:16

  • 7

    For a solution that works on Solaris, check out this link.

    – jahroy

    May 30, 2013 at 22:55


  • 137

    man grep | grep -C 1 context 🙂

    – StvnW

    Nov 3, 2015 at 3:57


  • 23

    man grep | grep -C 1 "\-C" 😉

    – Anders B

    Jul 7, 2016 at 10:56

  • 13

    @StvnW … I don’t know whether to call that meta (in a more general, rather than SO context), or what to call it. You answered the question by showing how to use the answer to find the answer.

    Sep 14, 2018 at 20:50


5209

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.

15

  • 59

    It 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

  • 9

    Ok, but what if want to show all lines of output after the match? grep -A0 and grep -A-1 don’t cut it…

    – user67416

    Jul 22, 2011 at 2:18

  • 2

    does not work for me for some reason, although mentioned in my man pages.

    Aug 1, 2012 at 9:43

  • 2

    If 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.

    – zkarthik

    Jan 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

642

-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).

4

  • 2

    I tried the -n format and found out it only works till 9. For 15 it returns 5 lines

    Mar 27, 2019 at 7:49

  • 17

    @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.

    – natiiix

    Apr 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

  • The -n option currently works for more than 9 lines on (GNU grep) 3.1.

    – mchid

    Jan 5 at 22:45

81

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

8

  • 6

    ack also supports -A -B.

    – Shuo

    Nov 4, 2016 at 7:49

  • 1

    grep has the advantage of being installed by default on many systems though.

    – Marc Z

    Oct 16, 2018 at 10:49

  • 3

    @MarcZ: True, grep is more likely to be installed by default than ack, but ack is a portable, stand-alone script. There’s no need to compile it or install it in a system directory such as /usr/bin/. Once it’s downloaded and placed into a directory listed in the $PATH (and its eXecute permission bit set), it should work right away. (No sudo or root privileges are required to get ack to work.)

    – J-L

    Nov 12, 2018 at 17:41

  • 1

    This question is about grep, not sure using it to “advertise” ack (which is a great tool, indeed) is a good idea…

    Dec 10, 2019 at 14:24

  • 2

    Why is it better for searching through code? Can you elaborate a little bit in your answer? (But without “Edit:”, “Update:”, or similar – the answer should appear as if it was written today.)

    Jul 17, 2021 at 22:01