- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- grep for string but include lines above and below
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 12:01 AM - last edited on тАО02-01-2012 04:16 AM by Ajay-Kumar
тАО01-05-2006 12:01 AM - last edited on тАО02-01-2012 04:16 AM by Ajay-Kumar
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
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 12:10 AM
тАО01-05-2006 12:10 AM
Re: grep for string but include lines above and below
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
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 12:24 AM
тАО01-05-2006 12:24 AM
Re: grep for string but include lines above and below
please have a look at:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=861863
Seems for multiple lines you require gnu grep.
- Tags:
- broken URL link
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 12:49 AM
тАО01-05-2006 12:49 AM
Solutiontry 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 01:47 AM
тАО01-05-2006 01:47 AM
Re: grep for string but include lines above and below
John .... did you omit to attach the sample script find_bef_after.sh ?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 04:23 AM
тАО01-05-2006 04:23 AM
Re: grep for string but include lines above and below
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 04:47 AM
тАО01-05-2006 04:47 AM
Re: grep for string but include lines above and below
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 06:36 AM
тАО01-05-2006 06:36 AM
Re: grep for string but include lines above and below
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 09:12 AM
тАО01-05-2006 09:12 AM
Re: grep for string but include lines above and below
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.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-05-2006 10:25 AM
тАО01-05-2006 10:25 AM
Re: grep for string but include lines above and below
It can be handled easily with ex as:
# ex -s input_file <
> EOF
cheers!
- Tags:
- ex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-25-2014 10:53 AM
тАО09-25-2014 10:53 AM
Re: grep for string but include lines above and below
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-26-2014 02:43 AM
тАО09-26-2014 02:43 AM
Re: grep for string but include lines above and below
>Where is the attached script?
Did you read every reply?
http://h30499.www3.hp.com/hpeb/attachments/hpeb/itrc-150/43222/1/270064.sh