简体   繁体   English

行之间的perl行匹配

[英]perl line matching between the lines

I have a file. 我有一个文件。

Sun Sep  9 12:34:42 2012 : Sun Sep  9 12:34:42 2012 : [Req] Send Bcast 
Sun Sep  9 12:34:32 2012 : RX FROM :152.14.189.4 MESG: 
Sun Sep  9 12:34:32 2012 : info  
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.4 
Sun Sep  9 12:34:32 2012 : RX FROM :13 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.13  
Sun Sep  9 12:34:32 2012 : RX FROM :9 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.9  
Sun Sep  9 12:34:32 2012 : RX FROM :14 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.14 
Sun Sep  9 12:34:32 2012 : RX FROM :5 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.5 
Sun Sep  9 12:34:35 2012 : Reply back to 9 
Sun Sep  9 12:34:35 2012 : Reply back to 13
Sun Sep  9 12:34:36 2012 : Reply back to 14
Sun Sep  9 12:34:37 2012 : Reply back to 1.1.1.4 
Sun Sep  9 12:34:37 2012 : Reply back to 5 
Sun Sep  9 12:34:42 2012 : Sun Sep  9 12:34:42 2012 : [Req] Send Bcast 
Sun Sep  9 12:34:32 2012 : RX FROM :152.14.189.4 MESG: 
Sun Sep  9 12:34:32 2012 : info  
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.5 
Sun Sep  9 12:34:32 2012 : RX FROM :13 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.9  
Sun Sep  9 12:34:32 2012 : RX FROM :9 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.4  
Sun Sep  9 12:34:32 2012 : RX FROM :14 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.13 
Sun Sep  9 12:34:32 2012 : RX FROM :5 
Sun Sep  9 12:34:32 2012 : #ip=1.1.1.14 
Sun Sep  9 12:34:35 2012 : Reply back to 9 
Sun Sep  9 12:34:35 2012 : Reply back to 14
Sun Sep  9 12:34:36 2012 : Reply back to 13
Sun Sep  9 12:34:37 2012 : Reply back to 4 
Sun Sep  9 12:34:37 2012 : Reply back to 5 

Between Two lines "Bcast" I need to search for a value say 1.1.1.4 and if it is present at both ip=1.1.1.1 and Reply back to 1.1.1.4. 在两行“ Bcast”之间,我需要搜索一个值,例如1.1.1.4,如果它同时存在于ip = 1.1.1.1处,然后回复1.1.1.4。 Then I need to ignore that set. 然后,我需要忽略该设置。 Else I need to write the differences in two separate files. 否则,我需要将差异写在两个单独的文件中。 I tried doing that in perl. 我尝试在perl中这样做。 When I to search between Bcast and Bcast, nothing is getting printed. 当我在Bcast和Bcast之间搜索时,什么都没打印出来。 Am i missing anything? 我有什么想念的吗? Here is what i have written 这是我写的

open my $in,'<',$ARGV[0];
open my $out,'>',"File2.txt";
my $Flag =0;
while(<$in>){
        if (/Bcast/ .. /Bcast/)
        {
          print ;
          if ( ($_ =~ m{ ip=1\.1\.1\.4 }xms) )
          {
                  $Flag=$Flag+1;
                  print " \nMatch is Found !!! Hurray \n" if ($Flag==2);
          }       
        } 
      $Flag=0;
}   

Kindly help me 请帮助我

/Bcast/ .. /Bcast/ will start and stop on the same line. /Bcast/ .. /Bcast/将在同一行开始和停止。 /Bcast/ ... /Bcast/ is a bit more promising, but then you realise you are skipping every second block. /Bcast/ ... /Bcast/更有希望,但是您意识到您正在跳过第二个块。

In reality, you are always in a Bcast block once you find the first one, so you shouldn't be trying to find out if you're in one using a flip-flop. 实际上,一旦找到第一个块,您就始终处于Bcast块中,因此您不应该尝试使用触发器来确定是否在一个块中。

# 0: Haven't see Bcast
# 1: Haven't seen "#ip=1.1.1.4" in this Bcast.
# 2: Haven't seen "Reply to 1.1.1.4" in this Bcast.
# 3: Found match
my $state = 0;  
while (<>) {   
    if (/Bcast/) {
       $state = 1;
    }

    if ($state == 1) {
       if (/ : \#ip=(\S+)/ && $1 eq '1.1.1.4') {
          $state = 2;
       }
    }
    elsif ($state == 2) {
       if (/ : Reply back to (\S+)/ && $1 eq '1.1.1.4') {
          $state = 3;
          print("Found match\n");
       }
    }
}

Note the your original code could accidentally match 1.1.1.43 . 请注意,您的原始代码可能会意外匹配1.1.1.43

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

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