Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

grep for string but include lines above and below

 
SOLVED
Go to solution
Derek Brown
Frequent Advisor

grep for string but include lines above and below

Hi, This forum has always been great for me and I wonder if anyone can solve this one for me please ?

I need to search a file for a specific string. Easy enough with grep e.g.

# grep root /etc/passwd

BUT .... I need the answer to provide the 3 lines above the string and also the 2 lines below the string.

Linux has this built in as a feature but is the same thing possible in standard HPUX ?

Many thanks.






 

 

-------------------------------

P.S. This Thread has been moved from HP-UX --> System Administration to HP-UX --> Languages & Scripting - Forum Moderator

11 REPLIES 11
Highlighted
Pete Randall
Outstanding Contributor

Re: grep for string but include lines above and below

Derek,

From "Handy One-Liners for Sed" (attached):

# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h


Pete

Pete
Peter Godron
Honored Contributor

Re: grep for string but include lines above and below

Derek,
please have a look at:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=861863
Seems for multiple lines you require gnu grep.
john korterman
Honored Contributor
Solution

Re: grep for string but include lines above and below

Hi,

try the attached script, find_bef_after.sh
using $1=string to grep for
$2=number of lines before and after string
$3=path to file

slow, but worked the last time I tried..

regards,
John K
it would be nice if you always got a second chance
Derek Brown
Frequent Advisor

Re: grep for string but include lines above and below

To John Korterman

John .... did you omit to attach the sample script find_bef_after.sh ?

Thanks
john korterman
Honored Contributor

Re: grep for string but include lines above and below

sorry, hope it appears now..
it would be nice if you always got a second chance
James R. Ferguson
Acclaimed Contributor

Re: grep for string but include lines above and below

Hi Derek:

How about a perl script like:

# cat ngrep
#!/usr/bin/perl
#@(#)ngrep $ Show +-3 lines matching target January 2006 JR_Ferguson $

use strict;
die "Usage: $0 pattern file\n" unless $#ARGV == 1;

my $pat = shift;
my $file = shift;
my @lines;
my $limit;
my ($i, $m, $n);

open( FH, "<", $file ) or die "Can't open $file : $!\n";
@lines = ;
$limit = $#lines;

foreach (@lines) {
if ( m/$pat/ ) {
$m = $i-3 < 0 ? 0 : $i-3;
$n = $i+3 > $limit ? $limit : $i+3;
for ( $m; $m <= $n; $m++ ) {
print $lines [$m];
}
print "\n";
}
$i++;
}
1;

Regards!

...JRF...
john korterman
Honored Contributor

Re: grep for string but include lines above and below

Hi again,

a variation taking four parameters - if it ever gets there:
$1 the string to match
$2 the number of lines to print before the matched string
$3 the number of lines to print after the matched string
$4 the file itself


regards,
John K.
it would be nice if you always got a second chance
Hein van den Heuvel
Honored Contributor

Re: grep for string but include lines above and below

JRF's solution will work fine a a file like /etc/passwd, but as it sucks in the whole file it will become problematic for very large (100+MB?) files.

I prefer a small circular buffer to just retain the last few lines.

For example:

awk 'BEGIN {W=4} {line[i++%W]=$0} /targetstring/{for (j=i-W;j
Or with a before-and-after:
awk 'BEGIN {W=5} {line[i++%W]=$0; if (1==x--){for (j=i-W;j
de-composed:
BEGIN set window size
Main loop
1) move current record into circular buffer
2) if counter x equals 1, then target was seen 1/2 a window lines ago, start print loop of memory array.
3) /target/... if target is seen, then set counter to 1/2 the window size.

( This code does NOT handle mutlipel adjacent matches )

I also have posted a perl script in:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=630972

Other prior discussions in:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=524659

Or a little further back in time:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=224110


btw...
Under OpenVMS it would be:
$SEARCH/WINDO=(before,after) file text


Cheers,
Hein.


Sandman!
Honored Contributor

Re: grep for string but include lines above and below

Derek,

It can be handled easily with ex as:

# ex -s input_file <> //-3,//+2 p
> EOF

cheers!
Sam_kao
Occasional Visitor

Re: grep for string but include lines above and below

Where is the attached script?

Re: grep for string but include lines above and below