比较两个文件中带有图案的线条

时间:2019-02-19 10:06:55

标签: perl

让我们举个例子

如果我的数据集是这样的。 LOG 1(x.log)包含

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

日志2(y.log)包含

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

然后第一行我要检查

################ start of test ################ ; T=1102266

################ start of test ################ ; T=1092507

由于T的值不相同,因此应在输出文件中提供这些详细信息,说明详细信息不匹配。

第二行,我们必须匹配

Checking the period of MTI, MTI10 clk from SV; T=1102334

Checking the period of MTI, MTI10 clk from SV; T=1092563

这里T的值也不匹配,因此将其传递到输出文件。

我必须在两个具有特定关键字mti_clk_chk的日志文件中逐行比较详细信息。到现在为止,我可以使用两个文件中的required关键字逐行解析它到第三个文件。现在,我想比较冒号(:)之后的关键字之后的数据,并在输出文件中,在与第二个数据集进行比较时,必须打印第一个数据集的不匹配行,并在第二个中比较行数第一数据集中不存在的数据集。解析两个日志文件后的数据如下。请帮我了解如何比较两组数据之间每一行中提供的详细信息。

open(FILE, "<x.log");
my @array = <FILE>;
close(FILE);

open(FILE, "<y.log");
my @array1 = <FILE>;
close(FILE);

open(FILE, ">>file.txt");
my @array2 = <FILE>;
foreach $_ (@array & @array1) {
         @array2 = grep {$_ =~ "mti_clk_chk:"} (@array);
                  print FILE "@array2";
          print FILE "\n \n \n";

         @array2 = grep {$_ =~ "mti_clk_chk:"} (@array1);
                  print FILE "@array2";
                  close(FILE);
exit;
}

在解析两个输入日志(x.log和y.log)之后对file.txt中的数据进行采样

INFO @576892 mti_clk_chk: run_stimulus called; T=576892
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
INFO @1102372 mti_clk_chk: Checking period of MTI CLk; T=1102372
INFO @1102377 mti_clk_chk: Period value of MTI Clock: 3.125000 ns; T=1102377
INFO @1102377 mti_clk_chk: MTI Clock is being generated correctly ; T=1102377
INFO @1102377 mti_clk_chk: Checking period of MTI10 CLk; T=1102377
INFO @1102418 mti_clk_chk: Period value of MTI10 Clock: 31.250000 ns; T=1102418
INFO @1102418 mti_clk_chk: MTI10 Clock is being generated correctly ; T=1102418
INFO @1102717 PHResourceLayer_Z4: mti_clk_chk: All clock period Checking done; T=1102717
INFO @1148661 mti_clk_chk: C-Code exit execution. code=<aa>; T=1148661
INFO @1148661 mti_clk_chk: ************************ SV END******************** ; T=1148661

UVM_INFO @0 reporter testbench.top_level_module.\mti_clk_chk::main : MTI_CLK_CHK_STIM Started .....; T=0
UVM_INFO @0 reporter testbench.top_level_module.\mti_clk_chk::main : run_stimulus called; T=0
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
UVM_INFO @1092598 reporter testbench.top_level_module.\mti_clk_chk::main : Checking period of MTI CLk; T=1092598
UVM_INFO @1092605 /proj/rru2_verif/usr/Tilak/SV_UVM/testbench/data_ipdss/v_ms_mti_stim_vip/testbench/classes_v/mti_clk_chk.sv(147) uvm_test_top.default_env.default_sequencer100@@mti_clk_chk mti_clk_chk:INFO: Period value of MTI Clock: 3.125000 ns; T=1092605
UVM_INFO @1092605 reporter testbench.top_level_module.\mti_clk_chk::main : MTI Clock is being generated correctly ; T=1092605
UVM_INFO @1092605 reporter testbench.top_level_module.\mti_clk_chk::main : Checking period of MTI10 CLk; T=1092605
UVM_INFO @1092655 /proj/rru2_verif/usr/Tilak/SV_UVM/testbench/data_ipdss/v_ms_mti_stim_vip/testbench/classes_v/mti_clk_chk.sv(165) uvm_test_top.default_env.default_sequencer100@@mti_clk_chk mti_clk_chk:INFO: Period value of MTI10 Clock: 31.250000 ns; T=1092655
UVM_INFO @1092655 reporter testbench.top_level_module.\mti_clk_chk::main : MTI10 Clock is being generated correctly ; T=1092655
UVM_INFO @1092850 reporter Z4_COREA: mti_clk_chk: All clock period Checking done; T=1092850
UVM_INFO @1092886 /proj/rru2_verif/usr/Tilak/SV_UVM/testbench/data_ipdss/v_ms_mti_stim_vip/testbench/classes_v/mti_clk_chk.sv(186) uvm_test_top.default_env.default_sequencer100@@mti_clk_chk mti_clk_chk:INFO: ************************ SV END******************** ; T=1092886

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您想要的输入数据

  • 从文件1中读取行
    • 过滤包含过滤器关键字mti_clk_chk:的行
    • 将所有内容存储在关键字后面以进行比较
  • 与文件2相同
  • 从文件1中打印出在文件2中找不到比较字符串的行
  • 反之亦然,文件2

针对您的问题的建议解决方案:

#!/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