grep a file, but show several surrounding lines?

grep lines before and after match linux
grep nth line after match
grep lines before and after match solaris
grep display second line
grep with multiple lines
grep multiple strings
grep lines after match until character
grep line number

I would like to grep for a string, but also show the preceding five lines and the following five lines as well as the matched line. How would I be able to do this?

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.

How do you grep a file and get the next 5 lines, Possible duplicate of grep a file, but show several surrounding lines? – Organic Context Line Control -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places With the -o or --only-matching option, this has no effect and a warning is given. -B NUM Some awk version. Here you are greping a file named FILE, for pattern MATCH -A is used to show lines below the match NUM stands for how many lines you wants to show below the match line, -B to show above the match line.

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

Show lines before and after with grep, Print num lines of leading and trailing context surrounding each match. The default is 2 and is equivalent to -A 2 -B 2. Note: no whitespace may be  DESCRIPTION cgrep provides all the features of grep, egrep, and fgrep, with greatly enhanced performance (see the section on PERFORMANCE) along with many additional features, one of which is the ability to output the context (surrounding lines) of the matching lines.

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

Grep: show lines before and after the match in Linux, You can add some additional parameters to your grep command, to search for keywords or phrases in files, to also show you the files that  Search String In A File Search String In Multiple Files. In previous example we have searched given string in a single file but real world problems are more than that. We can search string in multiple files by providing file name or extension with the help asterisk. In this example we will search in all text files by specifying *.txt file name.

grep astring myfile -A 5 -B 5

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

Quick Tip: Show Surrounding Lines With Grep, A quick tip, show the surrounding context of any grep match. A few days ago I made one of these small discoveries that make you An ajax call was failing randomly (aparently) with a 500 but I couldn't NAME grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [options] PATTERN [FILE. Little neat parameter for grep to show lines before and after the found line. -A number of lines to show after, -B number of lines to show before and -C numbers of lines to show before and after (with default of 2).

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

How to display lines 2-4 after each grep result?, not taken: mailbox unavailable (in reply to RCPT TO command) some content​. The simplest way to solve it using grep only, is to pipe one more inverted grep at the end. To print the next two lines as well, it is just a case of next and print n;​p twice more. sed -n '/The mail system/{n;n;N;N;p;i-- }' > output-file # or | . The standard option grep -l (that is a lowercase L) could do this. From the Unix standard: -l (The letter ell.) Write only the names of files containing selected lines to standard output.

How to grep a file, but also show the lines surrounding the match , Some, and probably most commercial versions of Unix grep to not have these switches and they only give you the single line containing the string match. From​  Display lines for a particular year in a file using grep hi, I have a log file with data for more than 3 years, i want only the rows for the year 2017, say for example. My file has the data like this 08-OCT-2015 11:17:35 AAA, BBBB 08-OCT-2017 11:17:35 AAA,Bdfdfd,dfdfd,dfd 08-Nov-2017 11:17:35 AAA,Bdfdfd,dfdfd,deree i want the rows

Can grep show context, but not a full line?, I have a file with several very long lines. I want to grep for a string which may occur several times in the file, including possibly more than once on the one line. $ cat  GREP AND: Match Multiple Patterns It is also often required to grep a file for multiple patterns – when it is needed to find all the lines in a file, that contain not one, but several patterns. Note, that you can both find the lines in a file that match multiple patterns in the exact order or in the any order.

how to grep and print the next N lines after the hit?, -C[num] Print num lines of leading and trailing context surrounding each match. Use the -A argument to grep to specify how many lines beyond the match to  You can use it like Grep in UNIX and Findstr in Windows with Select-String in PowerShell. Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.

Comments
  • I keep a copy of Brendan Gregg's perl script around for this purpose. Works well.
  • For a solution that works on Solaris, check out this link.
  • man grep | grep -C 1 context :)
  • man grep | grep -C 1 "\-C" ;)
  • @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.
  • 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
  • Ok, but what if want to show all lines of output after the match? grep -A0 and grep -A-1 don't cut it...
  • does not work for me for some reason, although mentioned in my man pages.
  • 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.
  • -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.
  • I tried the -n format and found out it only works till 9. For 15 it returns 5 lines