我正在编写一个 perl 脚本,它将查看 Oracle 11g 数据库警报日志并报告错误。我遇到问题的代码如下:
if ($_ =~ (m/WARNING/ or m/ORA/)){print $_ . "\n";}
我希望 if 语句(第 31 行)产生:
Mon Sep 01 01:01:01 1111 WARNING: THIS IS AN ERROR MESSAGE
Mon Sep 04 04:04:04 4444 WARNING: THIS IS ANOTHER ERROR MESSAGE
Mon Sep 05 05:05:05 5555 ORA-123456 HAS OCCURRED.
Mon Sep 06 06:06:06 6666 WARNING MESSAGE!
但是它正在生产:
Mon Sep 01 01:01:01 1111 WARNING: THIS IS AN ERROR MESSAGE
Mon Sep 02 02:02:02 2222 log switch has occurred
Mon Sep 03 03:03:03 3333 AUDIT: Purge complete
Mon Sep 05 05:05:05 5555 ORA-123456 HAS OCCURRED.
如果我为每个模式使用单独的 if 语句,我会得到想要的结果:
if ($_ =~ (m/WARNING/)){print $_ . "\n";}
if ($_ =~ (m/ORA/)){print $_ . "\n";}
我不知道为什么会这样。
数据:
Mon Sep 01 01:01:01 1111
WARNING: THIS
IS
AN
ERROR
MESSAGE
Mon Sep 02 02:02:02 2222
log switch
has
occurred
Mon Sep 03 03:03:03 3333
AUDIT: Purge complete
Mon Sep 04 04:04:04 4444
WARNING: THIS
IS
ANOTHER
ERROR
MESSAGE
Mon Sep 05 05:05:05 5555
ORA-123456 HAS
OCCURRED.
Mon Sep 06 06:06:06 6666
WARNING
MESSAGE!
脚本:
use strict;
use warnings;
my $input = $ARGV[0];
my $output = 'output.out';
my $log_line;
my @log_entries;
open INPUT, $input or die "Could not open $input: $!";
while(<INPUT>)
{
chomp;
if ($_ =~ m/^\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}/)
{
{
push (@log_entries, $log_line);
}
$log_line = "$_ ";
}
else
{
$log_line .= "$_ ";
}
}
push (@log_entries, $log_line);
close (INPUT);
foreach (@log_entries)
{
next if not defined($_);
if ($_ =~ (m/WARNING/ or m/ORA/)){print $_ . "\n";} #No idea why this doesn't work
# if ($_ =~ (m/WARNING/)){print $_ . "\n";}
# if ($_ =~ (m/ORA/)){print $_ . "\n";}
}