1

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

例如 :

文件1.txt

ID Value1
1   40
2   30
3   70

文件2.txt

ID Value2
1   50
2   70 
3   20 

以此类推,直到

文件 150.txt

ID Value150
1   98
2   52
3   71

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

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

谢谢你。

4

1 回答 1

4
  1. 使用剪切和粘贴组合解决三个或更多文件的文件合并问题。cd 到该文件夹​​仅包含 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
    
  2. 另一种解决方案是使用join逐步合并两个文件。

    join -j 1 test1 test2  | join -j 1 test3 - | join -j 1 test4 -
    
于 2013-01-15T00:19:57.260 回答