简体   繁体   English

Perl查找日志文件中是否有多个字符串,然后打印

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

Hi just learning Perl so please forgive. 嗨,您刚刚学习Perl,请原谅。 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. 我要尝试做的是如果日志文件包含一个或多个这些字符串Failed,Error或Skipped,然后打印出错误。(日志可能包含所有或全部一个或多个字符串)我尝试了以下代码的变体。 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 感谢您的帮助修改后的代码有效此解决方案使用数组#!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; #!perl = C:\\ IBM \\ RationalSDLC \\ ClearCase \\ bin \\ perl使用警告; 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; 另一个解决方案#!perl = C:\\ IBM \\ RationalSDLC \\ ClearCase \\ bin \\ perl使用警告; 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 解决方案#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); while (my $loglines = <$LOG>) { ... close($LOG);

Oh and this should be just LOG without the dollar signs. 哦,这应该只是LOG没有美元符号。

With use strict; use warnings; 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; use warnings; use strict; .

I would also modify your open calls to use the 3-arg, lexical scoped version. 我还将修改您的open调用以使用3-arg词汇作用域版本。

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: email_log.txt示例:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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