Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Print 5 lines before and after pattern match from a list

by bitingduck (Chaplain)
on Mar 14, 2015 at 16:33 UTC ( [id://1120063]=note: print w/replies, xml ) Need Help??


in reply to Print 5 lines before and after pattern match from a list

Edit: I managed to create a dummy file that printed according to what jayu_rao wanted, despite the double use of the counter noticed by hdb!

----original post below----

Are your caught lines not just too close together? Try adding the marker and counter that I put in below and look at the results. Also, if you make the match case insensitive you can compress the first three patterns into one. I tested it with the dummy file in the spoiler:

#!/usr/bin/env perl use strict; use diagnostics; open my $read_log, '<', '/home/jayu_rao/test_output/app.log' or die $! +; open my $write_tmp_app_log, '>', '/home/jayu_rao/test_output/write_tmp +_app.log' or die $!; my @patterns = ( qr/.*Error.*/, qr/.*error.*/, qr/.*ERROR.*/, qr/.*FATAL.*/, qr/.*Critical.*/, qr/.*exception.*/, ); my @lines=<$read_log>; my $i; my $counter=0; for (@patterns){ for $i (0..$#lines) { next unless $lines[$i] =~ $_; my $a = $i - 5 < 0 ? 0 : $i - 5;; my $b = $i + 5 > $#lines ? $#lines : $i + 5; for $i($a..$b){ print $write_tmp_app_log $lines[$i] }; $counter++; print $write_tmp_app_log "----------marker$counter------------"; } } close $read_log; close $write_tmp_app_log;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found