Shell: merge data

I am writing a shell script to combine data.
I have 2 files with different columns.
One of the columns is the same for both files.

Like :
File 1:
a   5
c   7
d   9
b   5

File 2:
c   1
d   8
a   6
b   3

At the moment, my script is putting data in a single file using

paste -d ' ' 'file1'  'file2' > "file3"

I would like to know if it is possible to combine two columns together and in order, like:

a   5   6
b   5   3
c   7   1
d   9   8

thank

+5
source share
3 answers
sort file1 > file1.tmp
sort file2 > file2.tmp

join -t " " -j 1 file1.tmp file2.tmp

It is assumed that the character and numbers are separated by a space

+2
source

Using process overrides , you can sort the files and combine them into one command.

join -t " " -j 1 <(sort file1) <(sort file2)
+1
source

sort , join .

0

All Articles