Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge two files by key if exists in the first file / bash script [duplicate]

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 :/

like image 782
Gregory Maris Avatar asked Dec 07 '22 15:12

Gregory Maris


1 Answers

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
like image 157
Kai Sternad Avatar answered Dec 11 '22 09:12

Kai Sternad