我想从多个文件中grep数据,然后将我grep的数据合并到一个日志中。我的输入文件如下:
Cell_a freq_100 50
Cell_a freq_200 6.8
Cell_b freq_100 70
Cell_a freq_100 100
Cell_a freq_200 10.5
Cell_b freq_100 60
<cell> <freq> <value_frm__file2> <value_frm_file1> <etc>
Cell_a freq_100 100 50
Cell_a freq_200 10.5 6.8
Cell_b freq_100 60 70
我只能一次grep一次这个值。有人可以帮忙解决这个问题吗?非常感谢!
答案 0 :(得分:1)
试试这个:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(switch say);
my %record;
while (<>) {
chomp;
my ($cell, $freq, $num) = split " ";
push @{$record{$cell}->{$freq}}, $num;
}
while (my ($cell, $freqs) = each %record) {
while (my ($freq, $nums) = each %$freqs) {
say "$cell $freq ", join(" ", @$nums);
}
}
像这样运行:
./t.pl input1.txt input2.txt