Email address obfuscation in effect -- please
click here to turn it off.
[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
- To: "MLUG Members" <EMAIL:PROTECTED>
- Subject: RE: [MLUG] grepping the 9th line after a matching line
- From: "McNutt, Justin M." <EMAIL:PROTECTED>
- Date: Fri, 4 May 2007 13:30:12 -0500
- Delivery-date: Fri, 04 May 2007 13:30:47 -0500
- Envelope-to: EMAIL:PROTECTED
- In-reply-to: <EMAIL:PROTECTED>
- Reply-to: MLUG Members <EMAIL:PROTECTED>
- Sender: EMAIL:PROTECTED
- Thread-index: AceOUr0tErR2eBONSeuANKEIB8LXhQAJbTAQ
- Thread-topic: [MLUG] grepping the 9th line after a matching line
The way you would do this in Perl is to set the line separator to
"undef" and then include the newlines in your pattern, like this:
undef $/; # "slurp mode"
if ( $input =~
/pattern[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n
[^\n]*\n([^\n]*)\n/ ) {
$ninthline = $1;
}
Basically, match your pattern, then match nine newlines in a row,
regardless of what is between the newlines, and grab what's between the
ninth and tenth newlines.
I'm not sure exactly how you would construct either the while loop to
get all the matches OR use a list to get all the matches.
Another thing I have done in Perl before is this. Remember that this
REQUIRES that ALL of the input is read into memory BEFORE any matching
can be done. Assume that you are running the program like: "NinthLine
<file>".
#!/usr/bin/perl
use strict;
use warnings;
if ( not defined $ARGV[0] ) {
die "Usage: $0 <file>\n\n";
}
# I'll leave out all the other error checking you should be doing here.
my $file = $ARGV[0];
my $index = 0;
my @CONTENTS;
open INFILE, "<$file";
PARSE: while ( <INFILE> ) {
push @CONTENTS[$index], $_;
if ( $index < 9 ) {
next PARSE; # Don't have enough data yet.
}
my $lookback = $index - 9;
if ( $CONTENTS[$lookback] =~ /pattern/ ) {
# Nine lines ago, we got something that matches our
pattern,
# so the CURRENT line is the one we want.
print $CONTENTS[$index];
}
$index++;
}
close INFILE; # Don't forget!
exit(0); # Don't forget this either!
You might be able to reconstruct this using an array in a shell script
where you read a file into an array then loop through as shown above
using 'grep' as your tester and 'echo' to print the lines you want.
--J
> -----Original Message-----
> From: EMAIL:PROTECTED
> [mailto:EMAIL:PROTECTED] On Behalf Of
> Stephen Montgomery-Smith
> Sent: Friday, May 04, 2007 8:46 AM
> To: MLUG Members
> Subject: Re: [MLUG] grepping the 9th line after a matching line
>
> Mike Miller wrote:
> > On Thu, 3 May 2007, Huggard, Arthur Charles (UMC-Student) wrote:
> >
> >> Correct me if I'm being too simplistic about this... but would
> >>
> >> egrep -A9 [pattern] file | tail -n1
> >>
> >> work?
> >
> >> nevermind... wouldn't work for multiple matches *duh*
> >
> >
> > Exactly. The second problem is if a second pattern match
> comes fewer
> > than 9 lines after the preceeding match, the behavior of
> egrep -A is
> > changed.
> >
>
> And that last issue will mess up my proposed solution also.
>
>
>
>
> _______________________________________________
> members mailing list
> EMAIL:PROTECTED
> http://mlug.missouri.edu/mailman/listinfo/members
>
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members