Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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中打印不带子字符串的字符串_Regex_String_Perl - Fatal编程技术网

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

Regex 在Perl中打印不带子字符串的字符串,regex,string,perl,Regex,String,Perl,我有一个日志文件,其格式如下: [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]

我有一个日志文件,其格式如下:

[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";
            }
            }
  }

如果
mmm
a.a.a
的顺序相同,则可以使用以下命令:

grep -v '.*\[mmm\].*?\[a\.a\.a\.a\]' file
如果你不知道顺序,就用这个

grep -P '^(?=.*\[mmm\])(?=.*?\[a\.a\.a\.a\]).+(*SKIP)(*F)|.+' file

您可以在perl中使用相同的正则表达式

perl -lne 'print if /^(?=.*\[mmm\])(?=.*?\[a\.a\.a\.a\]).+(*SKIP)(*F)|.+/' file
您可以使用:


前面的正则表达式将匹配包含
mmm
但不包含
a.a.a
的字符串,或者匹配包含
a.a.a.a
但不包含
mmm

的字符串不需要像
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.
}
use strict;
use warnings;

while(<DATA>){
    chomp;
    my @split = split(/\s+/);
    print "$_\n" unless ($split[1] eq '[mmm]' and $split[4] eq '[a.a.a.a]');
}


__DATA__
[Time] [mmm] [DATA] [rule] [a.a.a.a]
[Time] [ppp] [DATA] [rule] [a.a.a.a]
[Time] [mmm] [DATA] [rule] [c.c.c.c]
#/usr/bin/perl
使用警告;
严格使用;
我的@rule=(“[mmm]”,“[a.a.a.a]”);
my$regex=join'.*',map quotemeta,@rule;#从“规则”创建一个正则表达式。
$regex=qr/$regex/#编译它。
我的$c=0;
而(){
$c+=打印if/DATA/&!/$regex/;
最后如果$c>9;#只打印前10行。
}
您可以使用

grep -P "(?m)^(?!.*\[m{3}\].*\[a(?:\.a){3}\]).*$" file
见演示


对于不使用
map
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.
}
use strict;
use warnings;

while(<DATA>){
    chomp;
    my @split = split(/\s+/);
    print "$_\n" unless ($split[1] eq '[mmm]' and $split[4] eq '[a.a.a.a]');
}


__DATA__
[Time] [mmm] [DATA] [rule] [a.a.a.a]
[Time] [ppp] [DATA] [rule] [a.a.a.a]
[Time] [mmm] [DATA] [rule] [c.c.c.c]
使用严格;
使用警告;
while(){
咀嚼;
my@split=split(/\s+/);
除非($split[1]eq'[mmm]'和$split[4]eq'[a.a.a]',否则打印“$”;
}
__资料__
[时间][mmm][DATA][rule][a.a.a.a]
[时间][购买力平价][数据][规则][a.a.a.a]
[时间][mmm][DATA][rule][c.c.c.c]

你的意思是“[mmm]和[a.a.a.a]”还是“[mmm]或[a.a.a.a]”?@choroba我想是和。它是[mmm]和[a.a.a.a]@choroba