The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-20-2008
Registered User
 

Join Date: Jan 2008
Posts: 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.
Reply With Quote
Forum Sponsor
  #2  
Old 01-20-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
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.
Reply With Quote
  #3  
Old 01-20-2008
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
grep and display few lines before and after
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0