merge files using cut and paste

2019-06-04 05:18发布

问题:

i am trying to paste some large of files together. I would like to use paste. All the data has the same two column and i only want to print these two columns once.

For example

file1
h1 h2 i1
1 10 aa
2 20 bb
3 30 cc

file2
h1 h2 i2
1 10 xx
2 20 yy
3 30 zz

finaloutput
h1 h2 i1 i2
1 10 aa xx
2 20 bb yy
3 30 cc zz

I have around 3000 small files that need to be merged like this. I was wondering if there is a practical way of doing this? I can only think of using cut for each file and rename them

cut -f 3- myfile1 > myNewFile1
...
cut -f 3- myfile3000 > myNewFile3000

and then paste.

paste myNewFile* > FinalFile

I am hoping there is a more practical solution to this?

回答1:

Using join (limitation - can ONLY join on one column):

join -1 1 -2 1 -o 1.1 1.2 1.3 2.3 file{1,2}

Using awk:

awk 'NR==FNR { a[$1" "$2]=$0 ; next } ($1" "$2 in a) { print a[$1" "$2],$3 }' file{1,2}