根据匹配 file_2 中的前两列从 file_1 中提取行

根据匹配 file_2 中的前两列从 file_1 中提取行

文件_1

##chr   pos rc  allele_count    allele_states   deletion_sum    snp_type    most_variable_allele    diff:1-2    diff:1-3    diff:1-4    diff:1-5    diff:1-6    diff:1-7    diff:1-8    diff:1-9    diff:1-10   diff:1-11   diff:1-12   diff:2-3
MT  227 C   2   C/A 0   pop C   0   0   0   0   0   0   0.024   0   0.022   0   0   0
MT  233 G   2   G/T 0   pop G   0   0.009   0   0.012   0   0   0   0   0   0   0   0.009
MT  245 G   2   G/A 0   pop A   0   0   0   0   0   0.055   0.224   0.072   0.026   0   0   0
MT  251 C   2   C/T 0   pop C   0.276   0.034   0.231   0.005   0.027   0.036   0.025   0.002   0.107   0.034   0.034   0.309
MT  264 G   2   G/C 0   pop G   0   0   0   0.008   0   0.003   0   0   0   0   0   0
MT  286 G   2   G/T 0   pop T   0.002   0.002   0.002   0.002   0.002   0.002   0.002   0.002   0.002   0   0.002   0
MT  292 A   2   A/T 0   pop T   0   0   0   0   0.003   0   0   0.002   0   0   0   0
MT  293 G   2   G/T 0   pop G   0   0   0   0   0.003   0.002   0   0   0   0   0   0
MT  295 G   2   G/T 0   pop G   0   0.002   0.002   0   0.001   0.002   0.002   0.002   0.002   0.002   0.002   0.003

文件_2

MT  251
MT  292
MT  295

期望的输出

##chr   pos rc  allele_count    allele_states   deletion_sum    snp_type    most_variable_allele    diff:1-2    diff:1-3    diff:1-4    diff:1-5    diff:1-6    diff:1-7    diff:1-8    diff:1-9    diff:1-10   diff:1-11   diff:1-12   diff:2-3
MT  251 C   2   C/T 0   pop C   0.276   0.034   0.231   0.005   0.027   0.036   0.025   0.002   0.107   0.034   0.034   0.309
MT  292 A   2   A/T 0   pop T   0   0   0   0   0.003   0   0   0.002   0   0   0   0
MT  295 G   2   G/T 0   pop G   0   0.002   0.002   0   0.001   0.002   0.002   0.002   0.002   0.002   0.002   0.003

这与这篇文章中想要的类似:按第一列比较两个文件。如果匹配则保留行

我用于awk 'NR==FNR{a[$0]=$0;next}a[$0]'保持与第一列的重叠,但当它们与前两列(chr 和 pos)匹配时我需要整行。

答案1

您应该只测试前两列是否是数组的键,而不是使用 测试整行$0

awk 'NR==FNR {a[$1" "$2] = 1; next}
     FNR == 1 && FNR != NR {print} # print header
     $1" "$2 in a' File_2 File_1

相关内容