regex – 在Perl中打印不带子字符串的字符串

前端之家收集整理的这篇文章主要介绍了regex – 在Perl中打印不带子字符串的字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Logfile,其格式如下所示:

[Time] [mmm] [DATA] [rule] [a.a.a.a]
[Time] [ppp] [DATA] [rule] [a.a.a.a]
[Time] [mmm] [DATA] [rule] [c.c.c.c]

无法找到一种方法来打印这个没有特定子串的字符串.我希望能够在没有匹配[mmm]和[a.a.a.a]的子字符串行的情况下打印整个字符串输出.最终的输出是:

[Time] [ppp] [DATA] [rule] [a.a.a.a]  
[Time] [mmm] [DATA] [rule] [c.c.c.c]

我是否将索引模块与两个子字符串一起使用或者以某种方式使用grep?我是以错误的方式看待这个吗?任何帮助深表感谢!!!

在我的perl脚本中,我有一个搜索它的部分并将此部分打印为字符串:

sub Section
{
    my @event_rule = ("A","B","C");

    foreach (@event_rule)
    {
                    my $result1 = `fgrep -h 'DATA' $logfile1 | grep "$_" | head -n10`;
                    if (length $result1)
                    {
                        print "$result1\n";
            }
            }
  }

解决方法

不需要像grep这样的外部程序:

#!/usr/bin/perl
use warnings;
use strict;

my @rule  = ('[mmm]','[a.a.a.a]');
my $regex = join '.*',map quoteMeta,@rule; # Create one regular expression from the "rules".
$regex    = qr/$regex/;                      # Compile it.

my $c = 0;
while (<>) {
    $c += print if /DATA/ && ! /$regex/;
    last if $c > 9;                          # Only print the first 10 lines.
}

猜你在找的Perl相关文章