Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: Print line from file only once even if occurrence of pattern is more than once in the line

by Cristoforo (Curate)
on Mar 30, 2015 at 02:18 UTC ( [id://1121756]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Print line from file only once even if occurrence of pattern is more than once in the line
in thread Print line from file only once even if occurrence of pattern is more than once in the line

Note that the lines could occur immediately after one another and I would like to have output print only once in such cases.

To avoid this, a solution using vec could work for you.

The line of code vec($vec, $_, 1) ||= 1 for $lo .. $hi; would assign a 1 to each position. So you wouldn't repeat a line when printing out the results.

my @log_lines=<$read_tmp_log>; my $vec = ''; for my $i (0..$#log_lines) { next unless lc($log_lines[$i]) =~ /error|fatal|critical/; my $lo = $i - 5 < 0 ? 0 : $i - 5;; my $hi = $i + 5 > $#log_lines ? $#log_lines : $i + 5; vec($vec, $_, 1) ||= 1 for $lo .. $hi; } my $newline_needed = 0; for my $i (0 .. $#log_lines) { if (vec($vec, $i, 1) == 1) { print $log_lines[$i]; $newline_needed ||= 1; } else { print "\n" if $newline_needed; $newline_needed = 0; } }
  • Comment on Re^5: Print line from file only once even if occurrence of pattern is more than once in the line
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-23 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found