Command Line – How to Merge Files Using a Common Column

command linetext processing

I have two files out of which I want to create a third which contains all the information.

file 1:

a 111 
b 222 
c 333 
d 666 
e 777 

file 2:

111 x1  
222 x2
333 x3
444 x4 
555 x5 
666 x6 
777 x7 
888 x8

I would like to combine them as following:

111  x1  a
222  x2  b
333  x3  c
444  x4  0
555  x5  0
666  x6  d
777  x7  e
888  x8  0

Note:

The second column of file 1 is a subset of the first column of file 2

Best Answer

The join command does almost what you need, if the files are sorted as in your samples:

join -12 -a2 file1 file2 -o2.1,2.2,1.1

You just need to add the zeroes to the lines with no match. You can use the -e switch for that:

join -12 -a2 file1 file2  -o2.1,2.2,1.1 -e0
Related Question