merge files using cut and paste

落花浮王杯 提交于 2019-12-08 04:52:45

问题


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}


来源:https://stackoverflow.com/questions/16794986/merge-files-using-cut-and-paste

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!