使用perl对文件中的唯一高值进行排序

时间:2012-10-24 01:21:41

标签: regex perl

我有一个包含以下行的数据文件:

gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b171_b174_b236_b242_qreg/d2    19.33    18.65    -0.67 (VIOLATED)
gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b186_b196_b210_b223_qreg/d2    15.09    11.70    -3.39 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b103_b104/d2    40.35    36.61    -3.74 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b105_b106/d1    11.06     8.77    -2.29 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b105_b106/d1    40.08    29.18   -10.90 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b119_b120/d2    11.37     8.34    -3.03 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b11_b12/d2    14.56     4.41   -10.15 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b11_b12/d2    45.82    27.47   -18.34 (VIOLATED)

我需要获得第四列的唯一值和最小值以及相应的第一列。

输出应为:

gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b171_b174_b236_b242_qreg/d2    19.33    18.65    -0.67 (VIOLATED)
gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b186_b196_b210_b223_qreg/d2    15.09    11.70    -3.39 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b103_b104/d2    40.35    36.61    -3.74 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b105_b106/d1    40.08    29.18   -10.90 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b119_b120/d2    11.37     8.34    -3.03 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b11_b12/d2    45.82    27.47   -18.34 (VIOLATED)

我尝试使用模式匹配来获取散列中的第1列和第4列,然后对散列进行排序。但它不按我想要的方式工作。请帮忙。

while(<FILE1>) {
    my $path = $_;
    $path =~ /([^\s]+)/;
    $path = $1; #Extracting path
    chop($path);
    my $slack = $_;
    #$slack =~ /([^\s+]+)[\d+][\s+][\d+][\s+]([^\f+]+)[\s](VIOLATED)/;
    $slack =~ /[^\f+][\s+][\f+][\s+][\f+][\s+]([\f+]+)[\s](VIOLATED)/;
    $slack = $1;
    print "$slack\n";
    chop($slack);
    print FILE2 "$slack\n";
}

1 个答案:

答案 0 :(得分:0)

我会做类似的事情(如果文件名中没有空格):

my %res;
while(<DATA>) {
    chomp;
    my @vals = split/\s+/;
    $res{$vals[0]} = {} unless exists $res{$vals[0]};
    if (!exists($res{$vals[0]}{value}) || $vals[3] < $res{$vals[0]}{value}) {
        $res{$vals[0]}{value} = $vals[3];
        $res{$vals[0]}{line} = $_;
    }
}
say $res{$_}{line} for keys %res;

__DATA__
gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b171_b174_b236_b242_qreg/d2    19.33    18.65    -0.67 (VIOLATED)
gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b186_b196_b210_b223_qreg/d2    15.09    11.70    -3.39 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b103_b104/d2    40.35    36.61    -3.74 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b105_b106/d1    11.06     8.77    -2.29 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b105_b106/d1    40.08    29.18   -10.90 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b119_b120/d2    11.37     8.34    -3.03 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b11_b12/d2    14.56     4.41   -10.15 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b11_b12/d2    45.82    27.47   -18.34 (VIOLATED)

<强>输出:

gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b186_b196_b210_b223_qreg/d2    15.09    11.70    -3.39 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b105_b106/d1    40.08    29.18   -10.90 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b103_b104/d2    40.35    36.61    -3.74 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b119_b120/d2    11.37     8.34    -3.03 (VIOLATED)
gtssmpar11/dmunit1/mt_dm_fifo_csr_flopped_out/u_gt_ram/wrdataR_reg_0_b171_b174_b236_b242_qreg/d2    19.33    18.65    -0.67 (VIOLATED)
gtssmpar21/flunit1/flunitx1/fl_flex1/fl_mt_input_fifo/u_gt_ram/wrdataR_dreg_b11_b12/d2    45.82    27.47   -18.34 (VIOLATED)