Perl中的grep -m1函数,

时间:2018-02-05 21:21:03

标签: perl grep

对于有CASPER US_PARENT的行,我需要从组的第一行捕获时间和rootid并停止。

我需要跳过包含与CASPER US_PARENT的第一个实例相同的根顺序ID的其余行,并找到包含的第一行 相同的rootid但不同的apptype YIGYO。然后我需要比较CAPSER US_PARENT交易时间和YIGYO交易 时间,如果CASPER US_PARENT早于YIGYO, print the whole CAPSER US_PARENT`行。

timecompareperl.pl.viewCASPER

#!/usr/bin/perl

use strict ;
use warnings ;

use Time::Local ;
use List::Util ; 

#open (my $fh,'<:encoding(UTF-8)',$ARGV[0]) ;
#while (my $line = <$fh> ) {
while (my $line = <DATA> ) {

    my ( $msgcat, $transtme, $rootid, $clntid, $aptype, $evntyp ) = split(/,/, $line);

    if ( "$msgcat" eq "CASPER US_PARENT" ) {
        my $first_rootid = first { m{ $rootid }x } $line ;
        print "$first_rootid\n" 
        sleep 1 ;
    }
}

#msgcategory,transacttime,rootordid,clordid,apptype,eventtype

__DATA__



CASPER US_PARENT,2017.12.21T13:58:34.525,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.300,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.300,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:34.928,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
PARENT,2017.12.21T16:03:37.000,"""WMCE792e35c0992e:171221""","""A0318DC7-0443-410C-86FD-16D2989590B9""",YIGYO US,AR
PARENT,2017.12.21T16:03:37.806,"""WMCE792e35c0992e:171221""","""FA0A130A-DB35-4C13-B70A-0DEFFBF420FE""",YIGYO US,AR
CASPER US_PARENT,2017.12.21T13:58:34.552,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.303,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.303,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:34.936,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
PARENT,2017.12.21T16:03:35.369,"""WMCE14e8b1aab603:171221""","""7D695BC4-E771-4427-873E-CBC60DA09AD9""",YIGYO US,AR
PARENT,2017.12.21T16:03:35.793,"""WMCE14e8b1aab603:171221""","""BB3543B9-1581-4613-9781-4022DFDABA82""",YIGYO US,AR

Bash有一个grep -m/#/,它会找到一定数量的匹配然后停止。我没有在Perl grep函数中看到一个。我在List::Util中看到类似“grep one and done”行为的内容,但它无效。

1 个答案:

答案 0 :(得分:0)

所以,鉴于你所问的问题,这样的事情似乎可以解决问题:

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

use Time::Piece;
use Data::Dumper;

chomp( my @header = split /,/, <DATA> );

my %orders;

while (<DATA>) {
   chomp;
   my ( $msgcat, $transtme, $rootid, $clntid, $aptype, $evntyp ) = split /,/;

   $rootid =~ s/\"//g;
   $orders{$rootid}{$aptype} //= $transtme;
}

print Dumper \%orders;

foreach my $order ( keys %orders ) {

   foreach my $apid ( keys %{ $orders{$order} } ) {
      $orders{$order}{$apid} =~ s/\.(\d+)$//;
      my $msec = $1;
      print "$msec\n";

      my $time_convert =
        Time::Piece->strptime( $orders{$order}{$apid}, "%Y.%m.%dT%H:%M:%S" );
      $orders{$order}{$apid} = ( $time_convert->epoch * 1000 ) + $msec;
   }

   print "Time delta  for $order is: ",
     $orders{$order}{"YIGYO US"} - $orders{$order}{"CASPER US"}, "ms\n";
   ## in your case - look for a negative 'delta' as your test for comparison.
}


__DATA__
msgcategory,transacttime,rootordid,clordid,apptype,eventtype
CASPER US_PARENT,2017.12.21T13:58:34.525,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.300,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.300,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:34.928,"""WMCE792e35c0992e:171221""","""PUSA17122100ojwf""",CASPER US,AR
PARENT,2017.12.21T16:03:37.000,"""WMCE792e35c0992e:171221""","""A0318DC7-0443-410C-86FD-16D2989590B9""",YIGYO US,AR
PARENT,2017.12.21T16:03:37.806,"""WMCE792e35c0992e:171221""","""FA0A130A-DB35-4C13-B70A-0DEFFBF420FE""",YIGYO US,AR
CASPER US_PARENT,2017.12.21T13:58:34.552,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.303,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:31.303,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
CASPER US_PARENT,2017.12.21T16:05:34.936,"""WMCE14e8b1aab603:171221""","""PUSA17122100ojuz""",CASPER US,AR
PARENT,2017.12.21T16:03:35.369,"""WMCE14e8b1aab603:171221""","""7D695BC4-E771-4427-873E-CBC60DA09AD9""",YIGYO US,AR
PARENT,2017.12.21T16:03:35.793,"""WMCE14e8b1aab603:171221""","""BB3543B9-1581-4613-9781-4022DFDABA82""",YIGYO US,AR

它解析时间戳,将它们转换为纪元 - 有一点额外的处理毫秒 - 如果这不重要,那么你可以简化一点。

它给出的结果如下:

Time delta for WMCE792e35c0992e:171221 is: 7502475ms
Time delta for WMCE14e8b1aab603:171221 is: 7500817ms

您可以检查该delta为负值作为您的测试用例。