基于匹配列合并许多文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于匹配列合并许多文件相关的知识,希望对你有一定的参考价值。

我有很多文件(我以5个为例)

如果与第一个文件不匹配,则应在输出中追加0

填写1

1001 1 2
1002 1 2
1003 3 5
1004 6 7
1005 8 9
1009 2 3

文件2

1002 7
1003 8

维拉

1001 5
1002 3

文件4

1002 10
1004 60
1007  4

file5

1001 102
1003 305
1005 809

输出所需

1001 1 2 0 5  0 102
1002 1 2 7 3 10   0
1003 3 5 8 0  0 305
1004 6 7 0 0 60   0
1005 8 9 0 0  0 809
1007 0 0 0 0  4   0
1009 2 3 0 0  0   0

使用下面的代码我可以合并两个文件,但如何合并所有

awk 'FNR==NR{a[$1]=$2;next}{print $0,a[$1]?a[$1]:"0"}' file2 file1

1001 1 2 0
1002 1 2 7
1003 3 5 8
1004 6 7 0
1005 8 9 0

提前致谢

答案

使用GNU awk实现真正的多维数组和sorted_in:

$ cat tst.awk
FNR==1 { numCols = colNr }
{
    key = $1
    for (i=2; i<=NF; i++) {
        colNr = numCols + i - 1
        val   = $i
        lgth  = length(val)
        vals[key][colNr] = val
        wids[colNr] = (lgth > wids[colNr] ? lgth : wids[colNr])
    }
}
END {
    numCols = colNr
    PROCINFO["sorted_in"] = "@ind_num_asc"
    for (key in vals) {
        printf "%s", key
        for (colNr=1; colNr<=numCols; colNr++) {
            printf "%s%*d", OFS, wids[colNr], vals[key][colNr]
        }
        print ""
    }
}

$ awk -f tst.awk file*
1001 1 2 0 5  0 102
1002 1 2 7 3 10   0
1003 3 5 8 0  0 305
1004 6 7 0 0 60   0
1005 8 9 0 0  0 809
1007 0 0 0 0  4   0
1009 2 3 0 0  0   0
另一答案

GNU加入救援!

$ join -a1 -a2 -e '0' -o auto file1 file2 
  | join -a1 -a2 -e '0' -o auto - file3   
  | join -a1 -a2 -e '0' -o auto - file4   
  | join -a1 -a2 -e '0' -o auto - file5

选项-a1-a2告诉join插入缺少的字段。并且-e '0'告诉它用零替换它们。使用-o auto指定输出,该假定采用所有字段。

当拥有大量文件时,您无法使用管道构造,但您可以使用简单的for循环:

out=output
tmp=$(mktemp)
[[ -e "$out" ]] && rm -rf "$out" || touch "$out"
for file in f*; do
    join -a1 -a2 -e0 -o auto "$out" "$file" > "$tmp"
    mv "$tmp" "$out"
done
cat "$out"

或者如果你真的喜欢这个管道:

pipeline="cat /dev/null"
for file in f*; do pipeline="$pipeline | join -a1 -a2 -e0 -o auto - $file"; done
eval "$pipeline"

非常感兴趣:Is there a limit on how many pipes I can use?


备注:在这种情况下使用auto非常有用,但不是POSIX standard的一部分。它是GNU扩展,是GNU coreutils的一部分。一个纯POSIX版本会读起来有点麻烦:

$ join -a1 -a2 -e '0' -o 0 1.2 2.2 file1 file2 
  | join -a1 -a2 -e '0' -o 0 1.2 1.3 2.2 - file3 
  | join -a1 -a2 -e '0' -o 0 1.2 1.3 1.4 2.2 - file4 
  | join -a1 -a2 -e '0' -o 0 1.2 1.3 1.4 1.5 2.2 - file5

有关man join的更多信息

另一答案

使用GNU awk

awk '
NR>FNR && FNR==1{
colcount+=cols
}
{
for(i=2;i<=NF;i++){
  rec[$1][colcount+i-1]=$i
}
} 
{
cols=NF-1
}
END{
  colcount++
  for(ind in rec){
    printf "%s%s",ind,OFS
    for(i=1;i<=colcount;i++){
      printf "%s%s",rec[ind][i]?rec[ind][i]:0,OFS
    }
    print ""
  }
}' file{1..5} | sort -k1 | column -t

产量

1001  1  2  0  5  0   102
1002  1  2  7  3  10  0
1003  3  5  8  0  0   305
1004  6  7  0  0  60  0
1005  8  9  0  0  0   809
1006  0  0  0  0  0   666

注意:适用于here案例以及任何类型的值。

以上是关于基于匹配列合并许多文件的主要内容,如果未能解决你的问题,请参考以下文章

基于部分startswith匹配合并两个数据帧

基于唯一对组合将列从一个文件合并到另一个文件

根据列中的匹配数据合并两个 Excel 表

合并 Pandas Dataframe:如何添加列和替换值

用于合并 Excel 中与其他列中的信息匹配的行的单元格的宏

我有一个带有许多 textEdits 的片段,当我按下按钮时,这些字符串应该放在表格列中[重复]