4

i have two files with columns sorted by the value of the first column, and i want to merge them only if the value of the second exists in the first one.

The first file is like this

man01 xxx yyy zzz
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

The second file

man01 sss
man08 sss

And the desired output is

man01 xxx yyy zzz sss
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

I tried join but requires values of second file exist in the first one :/

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
Gregory Maris
  • 103
  • 2
  • 4

2 Answers2

15

Join can do this, have you considered the -a option ? It will produce a line for each unpairable file line in a.txt and b.txt.

join -a1 a.txt b.txt

man01 xxx yyy zzz sss
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz
Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
0

Well, it ain't a beauty of elegant command-line usage, but what about this?

perl -we 'open F1, "<file1.txt"; open F2, "<file2.txt"; my %f2 = map {/(\S+)/ ? ($1=>$_) : ()} <F2>; while (<F1>) { chomp; print; if (/(\S+)/ && exists $f2{$1}) { $f2{$1} =~ /\S+\s+(.*)/ and print " $1"; } print "\n"; }'

Update: While I was working on the above, it seems as though @bunting and @c00kiemon5ter came up with a much more elegant answer. Excellent!

thb
  • 13,796
  • 3
  • 40
  • 68