Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by jayu_rao (Sexton)
on Mar 29, 2015 at 17:16 UTC ( [id://1121711]=perlquestion: print w/replies, xml ) Need Help??

jayu_rao has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have written a code that matches the occurrence of the words like Error / Fatal etc from a log file and printing it to a temporary file.

However, the problem is that the words can occur more than once in a line like " ERROR - Critical error" or something like FATAL - Fatal error and this causes the lines to get printed more than once because of the patterns getting repeated more than once.

Can someone help me in suggesting a way to print the lines only once per occurrence in a line with an example?

Regards,

Jay

  • Comment on Print line from file only once even if occurrence of pattern is more than once in the line

Replies are listed 'Best First'.
Re: Print line from file only once even if occurrence of pattern is more than once in the line
by LanX (Saint) on Mar 29, 2015 at 17:48 UTC
    You most likely have a loop over lines doing different checks.

    Use next after the first successful match to parse the next line.

    Otherwise you should show us some code of what you tried, such that we understand were your problem is.

    See also How do I post a question effectively?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

      Thanks Rolf. You are right. The code snippet is as below:
      my @log_patterns = ( qr/ERROR/, qr/Error/, qr/FATAL/i, qr/critical/i, ); my @log_lines=<$read_tmp_log>; my $i; my $j; for (@log_patterns){ for $i (0..$#log_lines) { next unless $log_lines[$i] =~ $_; my $a = $i - 5 < 0 ? 0 : $i - 5;; my $b = $i + 5 > $#log_lines ? $#log_lines : $i + 5; print $output_file "\n"; for $j($a..$b){ print $output_file $log_lines[$j]; } } }
        Hi jayu_rao,

        Try to do this way:

        while(<$read_tmp_log>) { # reads input line if ( /error|fatal|critical/i ) { # if the line contains error, fata +l or critical case insensitively ... # process data } }
Re: Print line from file only once even if occurrence of pattern is more than once in the line
by kcott (Archbishop) on Mar 29, 2015 at 18:02 UTC

    G'day jayu_rao,

    You're going to have to show us some of this code you've written to get any sort of reasonable answer. The problem could be a major design flaw, a typo in a regex, or something else: we don't know.

    [See guidelines: How do I post a question effectively?]

    -- Ken

Re: Print line from file only once even if occurrence of pattern is more than once in the line
by CountZero (Bishop) on Mar 29, 2015 at 17:58 UTC
    It would indeed help if at least you show us the regex you are using.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Print line from file only once even if occurrence of pattern is more than once in the line
by sandy105 (Scribe) on Mar 30, 2015 at 11:16 UTC

    i wrote something similar a while back .

    @line = <filehandle> #filehandle used for opening the file foreach my $line (@lines) { #note i am matching a line doing something like printin +g and then using next .. if($line =~/^ProcessName:/){ $mess = substr $keyword , 13; $unirec{$id} .="$date,$id,$mess\n"; next; } if($line =~/^Process Message :/ ){ $mess = substr $keyword , 18; $unirec{$id} .="$date,$id,$mess\n"; next; } }

Log In?
Username:
Password:

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

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

    No recent polls found