让我们举一个例子
如果我的数据集是这样的。LOG 1(x.log)包含
INFO @ 1102266 PHResourceLayer_Z4:mti_clk_chk:################测试开始################; T = 1102266INFO @ 1102334 ……
如果我理解你想要的输入数据
mti_clk_chk:
针对您的问题提出的解决方案:
#!/usr/bin/perl use warnings; use strict; use autodie; use feature qw(say); die "usage: $0 <log1> <log2>\n" if @ARGV < 2; my($log1, $log2) = @ARGV; # log file extractor function sub extractor($) { my($file) = @_; my %lines; my @order; # Parse log file contents open(my $fh, '<', $file); while (<$fh>) { chomp; if (my($key) = /mti_clk_chk:\s*(.+)$/) { die "duplicate log line '$_' detected at ${file}:$.!\n" if exists $lines{$key}; $lines{$key} = $_; push(@order, $key); } } close($fh); return((\%lines, \@order)); } # parse log files my($lines_log1, $order_log1) = extractor($log1); my($lines_log2, $order_log2) = extractor($log2); # lines in log1 but not in log2 say foreach ( map { $lines_log1->{$_} } grep { ! exists $lines_log2->{$_} } @{ $order_log1 } ); # separator in output say ""; # lines in log2 but not in log1 say foreach ( map { $lines_log2->{$_} } grep { ! exists $lines_log1->{$_} } @{ $order_log2 } ); exit 0;
以您给出的两条线为例进行测试。我在开始和结束时添加了一些垃圾,以确保它不会以所需的输出结束。
$ cat dummy1.txt test1 INFO @1102266 PHResourceLayer_Z4: mti_clk_chk:################ start of test ################ ; T=1102266 INFO @1102334 PHResourceLayer_Z4: mti_clk_chk:Checking the period of MTI, MTI10 clk from SV; T=1102334 test1 $ cat dummy2.txt test2 UVM_INFO @1092507 reporter Z4_COREA: mti_clk_chk: ################ start of test ################ ; T=1092507 UVM_INFO @1092563 reporter Z4_COREA: mti_clk_chk: Checking the period of MTI, MTI10 clk from SV; T=1092563 test2 $ perl dummy.pl dummy1.txt dummy2.txt INFO @1102266 PHResourceLayer_Z4: mti_clk_chk:################ start of test ################ ; T=1102266 INFO @1102334 PHResourceLayer_Z4: mti_clk_chk:Checking the period of MTI, MTI10 clk from SV; T=1102334 UVM_INFO @1092507 reporter Z4_COREA: mti_clk_chk: ################ start of test ################ ; T=1092507 UVM_INFO @1092563 reporter Z4_COREA: mti_clk_chk: Checking the period of MTI, MTI10 clk from SV; T=1092563