I have a file test.txt on my linux system which has data in following format :
first second third fourth 10 first second third fourth 20 fifth sixth sevent
Perl solution:
perl -ne '/^(.*) (\S+)/ and push @{ $h{$1} },$2 }{ print "$_ @{$h{$_}}\n" for keys %h' < test.txt