<p>How do I <code>grep</code> and show the preceding and following 5 lines surrounding each matched line?</p>

[{'answer_id': 9083, 'author': 'Pat Notz', 'author_id': 825, 'author_profile': 'https://Stackoverflow.com/users/825', 'pm_score': 13, 'selected': True, 'text': '<p>For <strong>BSD</strong> or <strong>GNU</strong> <code>grep</code> you can use <code>-B num</code> to set how many lines before the match and <code>-A num</code> for the number of lines after the match.</p>\n\n<pre><code>grep -B 3 -A 2 foo README.txt\n</code></pre>\n\n<p>If you want the same number of lines before and after you can use <code>-C num</code>.</p>\n\n<pre><code>grep -C 3 foo README.txt\n</code></pre>\n\n<p>This will show 3 lines before and 3 lines after.</p>\n'}, {'answer_id': 9084, 'author': 'dbr', 'author_id': 745, 'author_profile': 'https://Stackoverflow.com/users/745', 'pm_score': 5, 'selected': False, 'text': '<pre><code>grep astring myfile -A 5 -B 5\n</code></pre>\n\n<p>That will grep "myfile" for "astring", and show 5 lines before and after each match</p>\n'}, {'answer_id': 9088, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 9, 'selected': False, 'text': '<p><code>-A</code> and <code>-B</code> will work, as will <code>-C n</code> (for <code>n</code> lines of context), or just <code>-n</code> (for <code>n</code> lines of context... as long as n is 1 to 9).</p>\n'}, {'answer_id': 9120, 'author': 'Sam Merrell', 'author_id': 782, 'author_profile': 'https://Stackoverflow.com/users/782', 'pm_score': 4, 'selected': False, 'text': '<p>I normally use</p>\n<pre><code>grep searchstring file -C n # n for number of lines of context up and down\n</code></pre>\n<p>Many of the tools like grep also have really great man files too. I find myself referring to <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?grep" rel="nofollow noreferrer">grep\'s man page</a> a lot because there is so much you can do with it.</p>\n<pre><code>man grep\n</code></pre>\n<p>Many GNU tools also have an <a href="http://web.archive.org/web/20090427121543/http://www.gnu.org:80/software/grep/doc/grep_toc.html" rel="nofollow noreferrer">info page</a> that may have more useful information in addition to the man page.</p>\n<pre><code>info grep\n</code></pre>\n'}, {'answer_id': 48325, 'author': 'elmarco', 'author_id': 1277510, 'author_profile': 'https://Stackoverflow.com/users/1277510', 'pm_score': 6, 'selected': False, 'text': '<p><a href="http://beyondgrep.com/" rel="noreferrer">ack</a> works with similar arguments as grep, and accepts <code>-C</code>. But it\'s usually better for searching through code.</p>\n'}, {'answer_id': 19237071, 'author': 'Malcolm Boekhoff', 'author_id': 1388639, 'author_profile': 'https://Stackoverflow.com/users/1388639', 'pm_score': 4, 'selected': False, 'text': '<p>Search for &quot;17655&quot; in <code>/some/file.txt</code> showing 10 lines context before and after (using <code>Awk</code>), output preceded with line number followed by a colon. Use this on Solaris when <code>grep</code> does not support the <code>-[ACB]</code> options.</p>\n<pre class="lang-sh prettyprint-override"><code>awk \'\n\n/17655/ {\n        for (i = (b + 1) % 10; i != b; i = (i + 1) % 10) {\n                print before[i]\n        }\n        print (NR &quot;:&quot; ($0))\n        a = 10\n}\n\na-- &gt; 0 {\n        print (NR &quot;:&quot; ($0))\n}\n\n{\n        before[b] = (NR &quot;:&quot; ($0))\n        b = (b + 1) % 10\n}\' /some/file.txt;\n</code></pre>\n'}, {'answer_id': 48225316, 'author': 'chtenb', 'author_id': 1546844, 'author_profile': 'https://Stackoverflow.com/users/1546844', 'pm_score': 4, 'selected': False, 'text': '<p>Use grep </p>\n\n<pre><code>$ grep --help | grep -i context\nContext control:\n  -B, --before-context=NUM  print NUM lines of leading context\n  -A, --after-context=NUM   print NUM lines of trailing context\n  -C, --context=NUM         print NUM lines of output context\n  -NUM                      same as --context=NUM\n</code></pre>\n'}, {'answer_id': 49772613, 'author': 'kenorb', 'author_id': 55075, 'author_profile': 'https://Stackoverflow.com/users/55075', 'pm_score': 3, 'selected': False, 'text': '<p>Here is the <a href="https://www.unix.com/302098992-post2.html?s=4309a3b1b120fabe0d81368129bc8921" rel="noreferrer">@Ygor solution</a> in <code>awk</code></p>\n\n<pre><code>awk \'c--&gt;0;$0~s{if(b)for(c=b+1;c&gt;1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}\' b=3 a=3 s="pattern" myfile\n</code></pre>\n\n<p><sup>Note: Replace <code>a</code> and <code>b</code> variables with number of lines before and after.</sup></p>\n\n<p>It\'s especially useful for system which doesn\'t support grep\'s <code>-A</code>, <code>-B</code> and <code>-C</code> parameters.</p>\n'}, {'answer_id': 49772704, 'author': 'kenorb', 'author_id': 55075, 'author_profile': 'https://Stackoverflow.com/users/55075', 'pm_score': 4, 'selected': False, 'text': '<h3><a href="https://github.com/BurntSushi/ripgrep" rel="noreferrer"><code>ripgrep</code></a></h3>\n\n<p>If you care about the performance, use <a href="https://github.com/BurntSushi/ripgrep" rel="noreferrer"><code>ripgrep</code></a> which has similar syntax to <code>grep</code>, e.g.</p>\n\n<pre><code>rg -C5 "pattern" .\n</code></pre>\n\n<blockquote>\n  <p><code>-C</code>, <code>--context NUM</code> - Show NUM lines before and after each match.</p>\n</blockquote>\n\n<p>There are also parameters such as <code>-A</code>/<code>--after-context</code> and <code>-B</code>/<code>--before-context</code>.</p>\n\n<p>The tool is built on top of <a href="https://github.com/rust-lang-nursery/regex" rel="noreferrer">Rust\'s regex engine</a> which makes it very <a href="http://blog.burntsushi.net/ripgrep/" rel="noreferrer">efficient</a> on the large data.</p>\n'}, {'answer_id': 54055064, 'author': 'JBone', 'author_id': 4969849, 'author_profile': 'https://Stackoverflow.com/users/4969849', 'pm_score': 2, 'selected': False, 'text': "<pre><code>$ grep thestring thefile -5\n</code></pre>\n\n<p><code>-5</code> gets you <code>5</code> lines above and below the match 'thestring' is equivalent to <code>-C 5</code> or <code>-A 5 -B 5</code>.</p>\n"}, {'answer_id': 55096777, 'author': 'prime', 'author_id': 2523147, 'author_profile': 'https://Stackoverflow.com/users/2523147', 'pm_score': 3, 'selected': False, 'text': '<p>Grep has an option called <code>Context Line Control</code>, you can use the <code>--context</code> in that, simply,</p>\n\n<pre><code>| grep -C 5\n</code></pre>\n\n<p>or</p>\n\n<pre><code>| grep -5\n</code></pre>\n\n<p>Should do the trick</p>\n'}, {'answer_id': 59291057, 'author': 'franzisk', 'author_id': 2716142, 'author_profile': 'https://Stackoverflow.com/users/2716142', 'pm_score': 3, 'selected': False, 'text': '<p>I do it the compact way:</p>\n<pre><code>grep -5 string file\n</code></pre>\n<p>That is the equivalent of:</p>\n<pre><code>grep -A 5 -B 5 string file\n</code></pre>\n'}, {'answer_id': 61486000, 'author': 'Rose', 'author_id': 466884, 'author_profile': 'https://Stackoverflow.com/users/466884', 'pm_score': 4, 'selected': False, 'text': '<p>If you search code often, <a href="https://github.com/ggreer/the_silver_searcher" rel="noreferrer">AG the silver searcher</a> is much more efficient (ie faster) than grep.</p>\n<p>You show context lines by using the <code>-C</code> option.</p>\n<p>Eg:</p>\n<pre><code>ag -C 3 &quot;foo&quot; myFile\n\nline 1\nline 2\nline 3\nline that has &quot;foo&quot;\nline 5\nline 6\nline 7\n</code></pre>\n'}, {'answer_id': 72938054, 'author': 'Tushar', 'author_id': 9730403, 'author_profile': 'https://Stackoverflow.com/users/9730403', 'pm_score': 3, 'selected': False, 'text': '<p><strong>Let\'s understand using an example.</strong><br>\nWe can use grep with options:</p>\n<pre><code>-A 5  # this will give you 5 lines after searched string.\n-B 5  # this will give you 5 lines before searched string.\n-C 5  # this will give you 5 lines before &amp; after searched string\n</code></pre>\n<p><strong>Example.</strong>\nFile.txt contains 6 lines and following are the operations.</p>\n<pre class="lang-bash prettyprint-override"><code>[abc@xyz]~/% cat file.txt # print all file data\nthis is first line\nthis is 2nd line\nthis is 3rd line\nthis is 4th line\nthis is 5th line\nthis is 6th line\n\n[abc@xyz]~% grep &quot;3rd&quot; file.txt # we are searching for keyword \'3rd\' in the file\nthis is 3rd line\n\n[abc@xyz]~% grep -A 2 &quot;3rd&quot; file.txt # print 2 lines after finding the searched string\nthis is 3rd line\nthis is 4th line\nthis is 5th line\n\n[abc@xyz]~% grep -B 2 &quot;3rd&quot; file.txt # Print 2 lines before the search string.\nthis is first line\nthis is 2nd line\nthis is 3rd line\n\n[abc@xyz]~% grep -C 2 &quot;3rd&quot; file.txt # print 2 line before and 2 line after the searched string \nthis is first line\nthis is 2nd line\nthis is 3rd line\nthis is 4th line\nthis is 5th line\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/9081', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/116/']