通过公共字段合并多个文件-Unix [英] Merge multiple files by common field - Unix

查看:91
本文介绍了通过公共字段合并多个文件-Unix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数百个文件,每个文件有两列:

I have hundreds of files, each with two columns :

例如:

file1.txt

file1.txt

ID Value1
1   40
2   30
3   70

file2.txt

file2.txt

ID Value2
1   50
2   70 
3   20 

依次类推,直到

file150.txt

file150.txt

ID Value150
1   98
2   52
3   71

如何根据第一列(常见)合并这些文件.我的输出应该是

How do I merge these files based on the first column (which is common). My output should be

ID Value1 Value2...........Value150
1   40     50                98
2   30     70                52
3   70     20                71

谢谢.

推荐答案

  1. 使用剪切和粘贴组合来解决三个或更多文件的文件合并问题. cd到该文件夹​​仅包含file1,file2,file3,... file150:

  1. using cut and paste combination to solve the file merging problem on three files or more. cd to the folder only contains file1, file2, file3, ... file150:

i=0
cut -f 1 file1 > delim   ## use first column as delimiter
for file in file*
do
        i=$(($i+1))  ## for adding count to distinguish files from original ones
        cut -f 2 $file > ${file}__${i}.temp
done
paste -d\\t delim file*__*.temp > output

  • 另一种解决方案是使用 join 一步一步地合并两个文件.

  • Another solution is using join to merge two files once by steps.

    join -j 1 test1 test2  | join -j 1 test3 - | join -j 1 test4 -
    

  • 这篇关于通过公共字段合并多个文件-Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆