简体   繁体   中英

Perl find whether more than one string is in a log file then print

Hi just learning Perl so please forgive. What I am trying to do is if the log file contains one or more of these strings Failed, Error or Skipped then print out an error.(the log could have one or more of all of the strings in the arrary or just one instance) I have tried variations on the below code. It compiles but it only prints out Done.

#!perl=C:\IBM\RationalSDLC\ClearCase\bin\perl

my @matches = (
  qr/"Failed"/,
  qr/"Skipped"/,
  qr/"Error"/,
 );

$mylog = "email_log.txt";

open (LOG, "<$mylog") || die 'Could not OPEN $mylog file';

LOG_READER:
while (my $loglines = <$LOG>) {
  if ($loglines =~ /@matches/) { 
   print "Error in the log\n";
   last LOG_READER;
  }
}
close($LOG);

print "\n Test is Done \n";

I have also tried

while ( $_ = <INPUT> ) {

  if( $_ =~ @matches ){
    print "\n Build Failed! \n";
  }
}

thank you for any assistance Modified code that works This solution uses an array #!perl=C:\\IBM\\RationalSDLC\\ClearCase\\bin\\perl

my @matches = qr/Failed | Skipped | Error/x;

my $mylog = "email_log.txt";

open (LOG, "<$mylog") || die 'Could not OPEN $mylog file';

LOG_READER:
while (my $loglines = <LOG>) {
  if ($loglines =~ /$matches/) { 
   print "Error in the log\n";
   last LOG_READER;
  }
}
close(LOG);

print "\n Test is Done \n";

This solution is what I used
#!perl=C:\\IBM\\RationalSDLC\\ClearCase\\bin\\perl use warnings; use strict;

my $myfailures = qr/Failed | Skipped | Error/x;
my $mylog = "email_log.txt";

open (LOG, "<$mylog") || die 'Could not OPEN $mylog file';

LOG_READER:
while (my $loglines = <LOG>) {
  if ($loglines =~ /$myfailures/) {
   print "Error in the log\n";
   last LOG_READER;
#    last;
  }
}

close(LOG);

print "\n Test is Done \n";

Another solution #!perl=C:\\IBM\\RationalSDLC\\ClearCase\\bin\\perl use warnings; use strict;

my $mylog = "email_log.txt";

open (INPUT, "<$mylog") || die 'Could not OPEN $mylog file';

LOG_READER:
while(<INPUT>) {
    next unless /(Failed|Skipped|Error)/;
    print "\n Build failed \n";
    last;
}

close(INPUT);

print "\n Test is Done \n";

Solution #4

#!perl=C:\IBM\RationalSDLC\ClearCase\bin\perl
use warnings;
use strict;

my $matches = "Failed|Skipped|Error";
my $mylog = "email_log.txt"; 

open my $LOG, "<", $mylog|| die 'Could not OPEN $mylog file';

while (my $loglines = <$LOG>) {
    if ($loglines =~ /$matches/) { 
        print "\n Error in the log\n";
        last;
    }
}
close($LOG);

print "\n Test is Done \n";

Thank you everyone for the contributions. They all worked really wish I could give everyone the correct answer. With the provided solutions I was able to get all the variations working.

if ($loglines =~ /@matches/) {

This is your problem. You cannot use an array as the regex to match against. Try this instead:

  if ($loglines =~ /"(Failed|Skipped|Error)"/) { 

while (my $loglines = <$LOG>) { ... close($LOG);

Oh and this should be just LOG without the dollar signs.

With use strict; use warnings; use strict; use warnings; you would get some better diagnostics. Get into the habit.

You are making this far too complicated. You really just need to do some reading on how regexps work, but its quite a simple problem to solve:

while(<INPUT>) {
    next unless /"(Failed|Skipped|Error)"/;
    print "\n Build failed \n";
    last;
}

You could use array of regex but for this particular case it's not needed,

my $matches = qr/"Failed" | "Skipped" | "Error"/x;

and then

if ($loglines =~ /$matches/) { .. }

First of all. I would always use use warnings; use strict; use warnings; use strict; .

I would also modify your open calls to use the 3-arg, lexical scoped version.

open my $fh, "<", $mylog; 

What exactly are you trying to match here?

my @matches = (
  qr/"Failed"/,
  qr/"Skipped"/,
  qr/"Error"/,
 );

An example from your log file would help. You're trying to match these keywords wrapped in actual quotes. Is this what you intended?

email_log.txt example:

Your build "Failed"
"Skipping" foo.c

This will match with or without surrounding quotes:

my $matches = "Failed|Skipped|Error";
my $mylog = "email_log.txt";
open my $fh, "<", $mylog; 

while (my $loglines = <$fh>) {
    if ($loglines =~ /$matches/) { 
        print "Error in the log\n";
        last;
    }
}
close($fh);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM