合并RHEL7.4上4个文件的(SUM)数据

时间:2018-03-06 16:52:48

标签: linux shell

我有4个不同的文件,每个文件具有相同的结构,然后调用101.txt到104.txt。这是一个片段作为示例

#Per-Day Traffic Summary
#-----------------------
#    date          received  delivered   deferred    bounced     rejected
#    --------------------------------------------------------------------
    Jan 31 2018     28131      34709       1305        392      34647 
    Feb  1 2018     32863      40714       1674        497      41694 
    Feb  2 2018     31113      38724       2353        543      41251 
    Feb  3 2018     28336      36913       3201        799      40219 
    Feb  4 2018     28442      36951       2257        865      39910 
    Feb  5 2018     29837      39448       1112        486      38566 
    Feb  6 2018     29844      38999       1609        497      39683 

我希望结合"收到"从每个文件,"交付"每天从每个文件等,以生成gnuplot的文件。例如,如果101和102在1月31日具有相同的金额,那么收到的将是56262.我总共有4个文件组合成一个具有相同结构的输出文件。

1 个答案:

答案 0 :(得分:0)

可以使用perl脚本完成:

perl sum.pl 101.txt 102.txt 103.txt 104.txt > result.txt

其中sum.pl包含:

#!/usr/bin/perl

my %processed_header;
my %data;

while (<>)
{
   if (/^(#.+)/)
   {
      print unless ($processed_header{$1});
      $processed_header{$1} = 1;
   }
   else
   {
      next unless(/(\w\w\w\s+\d+\s\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/);

      $data{$1}{'ord'} = scalar(keys(%data)) unless ($data{$1});

      $data{$1}{'rec'} += $2;
      $data{$1}{'del'} += $3;
      $data{$1}{'def'} += $4;
      $data{$1}{'bou'} += $5;
      $data{$1}{'rej'} += $6;
   }
}

for (sort { $data{$a}{'ord'} <=> $data{$b}{'ord'} } (keys(%data)))
{
   printf("%14s %10d %10d %10d %10d %10d\n",
          $_, $data{$_}{'rec'}, $data{$_}{'del'},
          $data{$_}{'def'}, $data{$_}{'bou'}, $data{$_}{'rej'});
}