![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| grep and display few lines before and after | melanie_pfefer | SUN Solaris | 8 | 09-25-2008 08:05 AM |
| grep + lines after | Janus | Shell Programming and Scripting | 4 | 11-03-2006 02:56 PM |
| grep string & next n lines | ashterix | Shell Programming and Scripting | 8 | 11-21-2005 08:38 PM |
| Grep on multiple lines | gundu | Shell Programming and Scripting | 13 | 03-25-2005 11:43 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
grep -n lines before and after
Hi,
is it possible to grep a pattern that will include the "n" lines before and after the line where the pattern have been found? e.g. #this contains the test.file line1 line2 line3 line4 line5 then a grep command to search the word "line3" and the output should be 1 (or n) line before that line and 1 (or n) line "after" that line. dessired output of the grep command line2 line3 line4 Thanks in advance. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
If you have GNU grep, then it is possible. From man grep
Code:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing -- between contiguous groups of
matches.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing -- between contiguous groups of
matches.
|
|
#3
|
||||
|
||||
| Google The UNIX and Linux Forums |