我有一个文件
head top_candidates
25 elevation_e gene1 20 9 0.0246022994932004 5 8 10.9217937824527
30 elevation_e gene1 59 18 0.0246022994932004 7 12 15.653559774527
31 elevation_e gene3 34 10 0.0246022994932004 6 9 9.47018201139585
108 elevation_e gene3 18 6 0.0246022994932004 4 7 6.86419248099239
和另一个文件
head genes.bed
Chr00c0001 52974 70567 gene1
Chr00c0003 32983 33237 gene2
Chr00c0003 36241 36792 gene3
Chr00c0003 100286 101468 gene4
Chr00c0004 80876 93710 gene5
当文件 2 (gene1,2,..) 的第 4 列与文件 1 的第 3 列匹配时,我想将第二个文件的第 1,2 和 3 列粘贴到第一个文件。
我想要的输出:
head desired
25 elevation_e gene1 20 9 0.0246022994932004 5 8 10.9217937824527 Chr00c0001 52974 70567
30 elevation_e gene1 59 18 0.0246022994932004 7 12 15.653559774527 Chr00c0001 52974 70567
31 elevation_e gene3 34 10 0.0246022994932004 6 9 9.47018201139585 Chr00c0003 36241 36792
108 elevation_e gene3 18 6 0.0246022994932004 4 7 6.86419248099239 Chr00c0003 36241 36792
答案1
怎么样
awk 'NR == FNR {T[$4] = $1 FS $2 FS $3; next} FNR == 1 {print "head desired"; next} {print $0, T[$3]}' file2 file1
head desired
25 elevation_e gene1 20 9 0.0246022994932004 5 8 10.9217937824527 Chr00c0001 52974 70567
30 elevation_e gene1 59 18 0.0246022994932004 7 12 15.653559774527 Chr00c0001 52974 70567
31 elevation_e gene3 34 10 0.0246022994932004 6 9 9.47018201139585 Chr00c0003 36241 36792
108 elevation_e gene3 18 6 0.0246022994932004 4 7 6.86419248099239 Chr00c0003 36241 36792