Aqui está outra abordagem Perl:
$ perl -ane 'foreach(@F[0..1]){$k{$F[2]}{$_}++}
END{
foreach $v (sort keys(%k)){
print "$_ " foreach(keys(%{$k{$v}}));
print "$v\n"
};
} ' file
Isso produz:
47723284 47196436 name1
42672249 430695 52856963 name2
380983 55094959 name3
34211 55584836 17926380 54321 3213456 name4
Explicação
OK, eu admito, o script Perl acima não é um exemplo fácil de entender Perl. Eu estou usando muitos truques e eles ofuscam o código. Estou apresentando a mesma solução aqui, mas formatada como um script e usando uma abordagem mais detalhada:
#!/usr/bin/perl
## This is the hash that will store our values.
my %k;
## Read through the input file line by line
## saving each line as $line. This is what the -n
## switch to perl means, only there each line is saved
## in the special variable $_.
while (my $line=<>) {
## Split the line into the @F array. This is
## what the -a switch does.
#chomp($line);
my @F=split(/\s+/,$line);
## Populate the %k hash that we defined at the beginning.
## This is a hash of hashes, it looks like this:
## $hash{key1}{key2}=value
## In this case, we are saying:
## $hash{3rd field}{1st field}=1
## $hash{3rd field}{2nd field}=1
## This just serves to add the 1st and 2nd fields
## to the list of fields for this $F[2] (the 3rd field, the name).
## A side effect of this is that hash keys are unique so duplicates
## are automatically removed.
$k{$F[2]}{$F[0]}=1;
$k{$F[2]}{$F[1]}=1;
}
## We have now finished processing the file
## (this is the END{} block above), so let's print.
## This saves the keys of the hash %k in the @names array
## sorted alphabetically.
my @names=(sort keys(%k));
## Go through each of the names, saving
## them as $name
foreach my $name (@names) {
## Now, iterate through the values associated
## with the current $name. These are saved as the
## keys of the hash %k{$name}
foreach my $value ( (keys(%{$k{$name}})) ){
print "$value ";
}
## Now print the name as well
print "$name\n";
}
O script acima faz exatamente a mesma coisa que o forro que eu postei, é apenas expandido para usar uma sintaxe mais clara.