您可以使用join
,但流水線變得如此複雜,可能更容易切換到更強大的Perl語言。
join -11 -21 -o1.1,1.2,1.3,1.4,1.5,2.4,2.5 \
<(sed 's/ \+/:/' fileA | sort) \
<(sed 's/ \+/:/' fileB | sort) \
| join -11 -22 -a1 -o1.1,1.2,1.3,1.4,1.5,1.6,1.7,2.5,2.6 \
- <(sed 's/ \+\([^ ]\+\) \+\([^ ]\+\)/ \1:\2/' fileC | sort -k2) \
| sed 's/:/ /'
Perl的解決方案,使用哈希記住所有的信息:
#!/usr/bin/perl
use warnings;
use strict;
# key_start key_end keep_from output
my %files = (A => [0, 1, 2, [0 .. 3]],
B => [0, 1, 2, [-2, -1]],
C => [1, 2, 3, [-2, -1]],
);
my %hash;
for my $file (keys %files) {
open my $FH, '<', "file$file" or die "file$file: $!";
while (<$FH>) {
my @fields = split;
$hash{"@fields[$files{$file}[0], $files{$file}[1]]"}{$file}
= [ @fields[$files{$file}[2] .. $#fields] ];
}
}
for my $key (sort keys %hash) {
print $key, join(' ', q(),
grep defined, map {
@{ $hash{$key}{$_} }[@{ $files{$_}[-1] }]
} sort keys %files), "\n";
}
[你嘗試過什麼到目前爲止(http://whathaveyoutried.com/)? – doublesharp