Eu faria isso em Perl:
#!/usr/bin/env perl
use strict;
my (%file1,%file2);
## Open the 1st file
open(A,"file1");
while(<A>){
## Remove trailing newlines
chomp;
## Split the current line on tabs into the @F array.
my @F=split(/\t/);
## This is the tricky part. It adds fields 2-last
## to the hash $file1. The value of this hash is an array
## and the keys are the 1st fields. This will result in a list
## of all 1st fields and all their associated columns.
push @{$file1{$F[0]}},@F[1..$#F];
}
## Open the 2nd file
open(B,"file2");
while(<B>){
## Remove trailing newlines
chomp;
## Split the current line on tabs into the @F array.
my @F=split(/\t/);
## If the current 1st field was found in file1
if (defined($file1{$F[0]})) {
## For each of the columns associated with
## this 1st field in the 1st file.
foreach my $col (@{$file1{$F[0]}}) {
print "$F[0]\t$col\t@F[1..$#F]\n";
}
}
}
Você pode jogar golfe em uma linha longa:
$ perl -lane 'BEGIN{open(A,"file1"); while(<A>){chomp; @F=split(/\t/);
push @{$k{$F[0]}},@F[1..$#F];} }
$k{$F[0]} && print "$F[0]\t@{$k{$F[0]}}\t@F[1..$#F]"' file2
1 today green a lot
1 today green sometimes
2 tomorrow at work
2 tomorrow at home
2 tomorrow sometimes
3 red new
Se você estiver trabalhando com arquivos grandes, deixe-o rodar um pouco.