Esta é uma solução de trabalho usando perl:
Crie um arquivo, por exemplo, mergeseeds.pl:
#!/bin/env perl
use List::Util qw[min max];
use Data::Dumper;
use constant COLUMNS=>3;
use constant SUM_COL=>2;
sub readfile($)
{
my $f=shift;
my @lines = 'cat $f';
chomp @lines;
return \@lines;
}
sub findseed($)
{
my $arr = shift;
my $line = 0;
for(@$arr)
{
return $line if(/SEED/);
$line++;
}
return $line;
}
sub process($$$)
{
my ($colnum,$numfiles,$line)=@_;
my $sum = 0;
my @nums = (split(/,/,$line.",END"));
while($colnum < scalar @nums-1)
{
$sum += $nums[$colnum-1];
$colnum+=COLUMNS;
}
print $line.",".$sum."\n";
}
sub popvalue($;@)
{
my ($arr,@filler)=@_;
return @$arr ? (shift @$arr) : (@filler);
}
sub pad_array($$$)
{
my ($arr,$pad,$filler)=@_;
while ($pad--)
{
unshift @$arr, $filler;
}
}
sub pad_arrays($$)
{
my ($files,$pads)=@_;
for(@{$files})
{
pad_array($_,shift @$pads,",,");
}
}
sub merge_files(@)
{
my @files=@_;
my $numfiles = scalar @files;
my @seedsfound = map { findseed($_); } @files;
my $maxseed = max(@seedsfound);
my @padcounts = map { ($maxseed - $_); } @seedsfound;
pad_arrays(\@files,\@padcounts);
my $maxlines = max( map { scalar @$_; } @files);
my $line= 0;
while($line < $maxlines)
{
my @items = map {popvalue($_,",,"); } (@files);
process(SUM_COL,$numfiles,join(",",@items));
$line++;
}
}
sub read_files(@)
{
my @filenames=@_;
my @files = map { readfile($_); } @filenames;
return @files;
}
sub usage($)
{
my ($msg)=(@_);
print STDERR "usage: $0 ≤Filename>...\n";
print STDERR $msg."\n";
exit 1;
}
sub main(@)
{
my @fnames=();
for my $f (@_)
{
if(! -f $f)
{
usage( "ERROR: not a file:$f\n");
}
push(@fnames,$f);
}
merge_files(read_files(@fnames));
}
main(@ARGV);
Em seguida, ligue a partir da linha de comando:
perl mergeseeds.py File*.seed