- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- searching script help
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
Forums
Discussions
Discussions
Discussions
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
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
12-19-2006 10:42 PM
12-19-2006 10:42 PM
I've a file as follows:
2006-1-1
a 1 2 3 4 5 6
b 1 2 3 4 5 6
c 1 2 3 4 5 6
d 1 2 k 4 5 6
2007-1-2
a 1 2 g 4 5 6
d 1 2 t 4 5 6
d 1 2 t 4 5 6
e 1 2 r 4 5 6
2006--1-3
d 1 2 3 4 5 6
e 1 2 g 4 5 6
a b c d e f g
c v f 3 4 5 6
I would like to search the file to print
2006--1-3
d 1 2 3 4 5 6
e 0 0 0 4 5 6
a b c d e f g
based on first occurance of the input search string
e 0 0 0 4 5 6
ie. basically
on greping the first occurance of
e 0 0 0 4 5 6
how can I print the previous say 20 lines,
or
how can I print the previous lines up to the header 2006
Thanks for all help...
Bill
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2006 11:52 PM
12-19-2006 11:52 PM
Re: searching script help
[ Long time, no hear :-)) ]
OK, try this Perl script:
# cat ./findpat
#!/usr/bin/perl
use strict;
use warnings;
my $pat = shift;
my @lines;
while (<>) {
shift @lines if $#lines > 20;
if (/$pat/) {
print for @lines;
print;
last;
}
push @lines,$_;
}
...Run it passing the pattern to match as the first argument and the file to process as the second:
# ./findpat 2006--1-3 filename
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2006 12:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2006 12:11 AM
12-20-2006 12:11 AM
Re: searching script help
I'm sorry, but I don't understand the requirement.
e 0 0 0 4 5 6 is not in the file, so a grep would return nothing.
Might be easiest to get gnu grep which has a -B n option, showing n lines before the matched line.
For context grep/sed/perl solutions:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=861863
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=793890
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2006 12:44 AM
12-20-2006 12:44 AM
Re: searching script help
Here is also a perl script:
# cat testing.pl
#!/usr/bin/perl -w
my $search=shift;
my (@saves,@lines);
while (<>) {
if ($_ =~ /$search/) {
foreach $i (0..$#saves)
{
if ($saves[$i] =~ /^\d{4}-\d{1}-\d{1}/) {
unshift(@lines,$saves[$i]);
last;
} else {
push(@lines,$saves[$i]);
}
}
push(@lines,$_);
if ($#lines > 0){
foreach $i (0..$#lines)
{
print $lines[$i];
}
}
@lines = ();
@saves = ();
} else {
unshift(@saves,$_);
}
}
# perl testing.pl "c v f 3 4 5 6" filename
2006-1-3
a b c d e f g
e 1 2 g 4 5 6
d 1 2 3 4 5 6
c v f 3 4 5 6
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2007 08:52 PM
06-13-2007 08:52 PM