The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 13 02-19-2009 11:54 PM
grep + lines after Janus Shell Programming and Scripting 4 11-03-2006 05:56 PM
grep string & next n lines ashterix Shell Programming and Scripting 8 11-21-2005 11:38 PM
Grep on multiple lines gundu Shell Programming and Scripting 13 03-25-2005 02:43 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-21-2008
airman_ole airman_ole is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
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.
  #2 (permalink)  
Old 01-21-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 (permalink)  
Old 01-21-2008
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
grep and display few lines before and after
  #4 (permalink)  
Old 07-03-2009
Sumedha Sobti Sumedha Sobti is offline
Registered User
  
 

Join Date: Jul 2009
Location: India
Posts: 9
Hi,

There is a very simple way of doing this. Lets say you have a file test.txt having 'view' in some line. Say, you wish to get 5 lines above and below the line containing 'view' and output to a file say test_one.txt use the following:

grep -C 5 "view" test.txt > test_one.txt

Regards,
Sumedha
  #5 (permalink)  
Old 07-03-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
Please note: a lot of these examples ONLY work with GNU tools, not all versions of grep.
  #6 (permalink)  
Old 07-05-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
hope below perl can help you some

Code:
sub lines_grep{
my($pattern,$line,$flag,$n,@tmp)=(@_);
while(<DATA>){
	if($_=~/$pattern/){
		print @tmp;	
		$flag=1;
	}
	else{
		if($#tmp < $line-1){
			push @tmp, $_;
		}
		else{
			shift @tmp;
			push @tmp, $_;
		}
	}
	if ($flag==1){
		print $_ ;
		$n++;
	}
	if($n>$line){
		last;
	}
}
}
#lines_grep(pattern,3);
lines_grep(4,2);
__DATA__
1
2
3
4
pattern
6
7
8
9
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0