Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用模式匹配的奇怪perl foreach行为_Regex_Arrays_Oracle_Perl - Fatal编程技术网

Regex 使用模式匹配的奇怪perl foreach行为

Regex 使用模式匹配的奇怪perl foreach行为,regex,arrays,oracle,perl,Regex,Arrays,Oracle,Perl,我正在编写一个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 Mo

我正在编写一个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";}
}
脚本

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";}
}
使用严格;
使用警告;
my$input=$ARGV[0];
my$output='output.out';
我的$log_行;
我的@log_条目;
打开输入,$INPUT或die“无法打开$INPUT:$!”;
while()
{
咀嚼;
如果(${u0=~m/^\w{3}\w{3}\d{2}\d{2}:\d{2}:\d{2}\d{4}/)
{
{
推送(@log\u条目,$log\u行);
}
$log\u line=“$”;
}
其他的
{
$log\U行。=“$”;
}
}
推送(@log\u条目,$log\u行);
关闭(输入);
foreach(@log\u条目)
{
如果未定义,则为下一步($\ux);
如果($\=~(m/WARNING/或m/ORA/){print$\.\n”;}#不知道为什么这不起作用
#如果($\=~(m/WARNING/){print$\.\n”;}
#如果($\=~(m/ORA/){print$\.\n”;}
}

这是使用or操作将这两行组合在一起的方式

if ($_ =~ (m/(WARNING|ORA)/)){print $_ . "\n";}

您必须在匹配结果上定义布尔表达式,而不是匹配运算符-使用

if (($_ =~ m/WARNING/) or ($_ =~ m/ORA/)) {print $_ . "\n";}
而不是

if ($_ =~ (m/WARNING/ or m/ORA/)){print $_ . "\n";}
缺少绑定运算符(
=~
!~

意味着

也就是说

$_ =~ (m/WARNING/ or m/ORA/)
意味着

那么你最终会做什么

$_ =~ ''

这显然不是你想要的。请改用下列方法之一:

$_ =~ m/WARNING/ or $_ =~ m/ORA/
   -or-
/WARNING/ || /ORA/
   -or-
/WARNING|ORA/

我不知道Perl有一个OR运算符@Simon Catlin,
的行为就像
|
一样。区别在于
的优先级远远低于
|
<代码>或通常用于流量控制(例如,
打开(…)或模具$!
)。操作员记录在感谢中,回答非常好。我真的很挣扎。这就解释了为什么我必须输入:next if not defined($);我一直在串联(.)或字符串中使用未初始化的值$
$_ =~ '1'
$_ =~ m/WARNING/ or $_ =~ m/ORA/
   -or-
/WARNING/ || /ORA/
   -or-
/WARNING|ORA/