使用剪切和粘贴合并文件

使用剪切和粘贴合并文件

本文介绍了使用剪切和粘贴合并文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些大文件粘贴在一起.我想使用粘贴.所有数据都具有相同的两列,我只想将这两列打印一次.

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.

例如

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

我大约需要合并3000个小文件.我想知道是否有可行的方法?我只能考虑对每个文件使用cut并重命名它们

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

然后粘贴.

paste myNewFile* > FinalFile

我希望对此有更实际的解决方案?

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

推荐答案

使用join(限制-可以加入一列):

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}

使用awk:

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

这篇关于使用剪切和粘贴合并文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:35