如何在awk中使用键列连接两个文件

如何在awk中使用键列连接两个文件

我有两个表格文件,我想像这样加入它们:

文件A.out

Well    Sample Name Sample Color    
A1  B1000-1-KF  "RGB(132,193,241)"
A1  B1000-1-KF  "RGB(132,193,241)"      
A2  B500-3-KF   "RGB(168,255,222)"      
A2  B500-3-KF   "RGB(168,255,222)"

文件B.out

Well    Cycle   Target Name Rn  ΔRn
A1  1   Ngene   433375.375  -2322.9575
A1  2   Ngene   435992.3438 -1616.2767
A1  3   Ngene   438522  -996.9085
A1  4   Ngene   441353  -76.1964
A2  1   Ngene   541226.1875 13234.6875
A2  2   Ngene   514756.8125 -13234.6875
A2  3   Ngene   517349.2188 -10642.2812
A2  4   Ngene   521414.875  -6576.625

我想要 C.out :

Well    Cycle   Target Name Rn  ΔRn Sample Name
A1  1   Ngene   433375.375  -2322.9575  B1000-1-KF
A1  2   Ngene   435992.3438 -1616.2767  B1000-1-KF
A1  3   Ngene   438522  -996.9085   B1000-1-KF
A1  4   Ngene   441353  -76.1964    B1000-1-KF
A2  1   Ngene   541226.1875 13234.6875  B500-3-KF
A2  2   Ngene   514756.8125 -13234.6875 B500-3-KF
A2  3   Ngene   517349.2188 -10642.2812 B500-3-KF
A2  4   Ngene   521414.875  -6576.625   B500-3-KF

所以每个文件中的第一列是关键,我想放置所有 B.out 和 A.out 的第二列。

到目前为止我已经做了:

awk -F'\t' -vOFS="\t" 'NR==FNR{a[$1]=$1;next}{print $0,a[$2]} B.out A.out > C.out

但它不起作用,有什么想法吗?

谢谢

答案1

尝试

awk -F'\t' -vOFS="\t" 'NR==FNR{a[$1]=$2;next}{print $0,a[$1]} ' A.out B.out

在哪里

  • $1是第一个文件中的键和$2
  • 在第二个文件中,值是从键中获取的$1

这给了我

Well    Cycle   Target Name Rn  ΔRn Sample Name Sample Color
A1  1   Ngene   433375.375  -2322.9575  B1000-1-KF
A1  2   Ngene   435992.3438 -1616.2767  B1000-1-KF
A1  3   Ngene   438522  -996.9085   B1000-1-KF
A1  4   Ngene   441353  -76.1964    B1000-1-KF
A2  1   Ngene   541226.1875 13234.6875  B500-3-KF
A2  2   Ngene   514756.8125 -13234.6875 B500-3-KF
A2  3   Ngene   517349.2188 -10642.2812 B500-3-KF
A2  4   Ngene   521414.875  -6576.625   B500-3-KF

答案2

命令

for i in A1 A2; do o=`awk -v  i="$i" '$1 == i {if(!seen[$2]++){print $2}}' f1`; awk -v i="$i" -v o="$o"  'BEGIN {print "Well    Cycle   Target Name Rn  ΔRn  Sample Name" } $1 == i {print $0,o}' f2; done

输出

Well    Cycle   Target Name Rn  ΔRn  Sample Name
A1  1   Ngene   433375.375  -2322.9575 B1000-1-KF
A1  2   Ngene   435992.3438 -1616.2767 B1000-1-KF
A1  3   Ngene   438522  -996.9085 B1000-1-KF
A1  4   Ngene   441353  -76.1964 B1000-1-KF
Well    Cycle   Target Name Rn  ΔRn  Sample Name
A2  1   Ngene   541226.1875 13234.6875 B500-3-KF
A2  2   Ngene   514756.8125 -13234.6875 B500-3-KF
A2  3   Ngene   517349.2188 -10642.2812 B500-3-KF
A2  4   Ngene   521414.875  -6576.6 B500-3-KF

相关内容