Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: grab 'n' lines from a file above and below a /match/

by Anonymous Monk
on Sep 17, 2004 at 20:19 UTC ( [id://391886]=note: print w/replies, xml ) Need Help??


in reply to grab 'n' lines from a file above and below a /match/

# In fat crayon mode for your pleasure # spoofing your big log file handle as a little array for brevity my @logs = (); for(1..20){push(@logs, $_)} my $lookBack = 2; # the number of lines behind the match you want my $lookAhead = 2; # the number of lines ahead the match you want my $matchReg = '9'; # what your matching on (can be inlined) my $matched = 0; # flag when we have a match my @buffer = (); # your lookBack buffer # Iterate through the logs looking for match foreach my $line (@logs) { # Feed the buffer push(@buffer, $line); # Trim the buffer shift(@buffer) if $#buffer > ($lookBack-1); # Do stuff if this line matches if ($line =~ /$matchReg/) { # Print the buffer for(@buffer){print "lookback: $_\n"} # Wave a flag we have a match $matched = $lookAhead; # Nothing else to see here, move along next; } # Still working the lookAhead from a prior match? if($matched){ # one less match --$matched; print "LookAhead: $line\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391886]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found