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:40:27 -0500
- Delivery-date: Fri, 04 May 2007 13:40:54 -0500
- Envelope-to: EMAIL:PROTECTED
- In-reply-to: <EMAIL:PROTECTED>
- Reply-to: MLUG Members <EMAIL:PROTECTED>
- Sender: EMAIL:PROTECTED
- Thread-index: AceOUr0tErR2eBONSeuANKEIB8LXhQAJbTAQAACOUDA=
- Thread-topic: [MLUG] grepping the 9th line after a matching line
Improvement (memory efficiency):
#!/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 $index = 0;
my @CONTENTS;
open INFILE, "<$ARGV[0]";
PARSE: while ( <INFILE> ) {
push @CONTENTS, $_; # Fixed useless index.
if ( $index < 9 ) {
next PARSE; # Don't have enough data yet.
}
# We now have a ten-line buffer.
if ( $CONTENTS[0] =~ /pattern/ ) {
print $CONTENTS[9];
}
# Clear out the first line. Already checked it.
# This makes it so ONLY ten lines are in memory
# at any given time. You might be able to use
# undef here instead of a dummy scalar.
my $devnull = shift @CONTENTS;
$index++; # Just in case you still want the
# actual line number (don't forget
# to add 1).
}
close INFILE; # Don't forget!
exit(0); # Don't forget this either!
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members