我是 awk 的新手,尝试比较 file1 和 file 2 之间的“:”分隔的列 1(例如,chr10)和 2(例如,10000003);并使用 awk 将匹配的行写入新文件中。
文件一:
chr10:10000003 chr10:10000005 chr10:10000015 chr10:10000017 chr10:100000202 chr10:10000033 chr10:100000380 chr10:10000043 chr10:100000465 chr10:10000052
文件2:
chr1:1806476:T/C:-2.12680332451125 0.835119313863368\ chr1:1806503:空调:-1.56871277809939 0.764924263070418\ chr10:10000003:C/T:-0.572267893158369 0.607055146639116\ chr1:1825420:C/T: 1.70588504817348 0.22407517592607\ chr1:2019496:G/C: 2.34709890656509 0.147215274051584\ chr1:2019501:C/T:-2.06157612494769 0.82769600171016\ chr10:100000202:C/A: 0.808838763489275 0.362093542746135\ chr1:2028192:G/A:-0.164564659049733 0.534780784989026\ chr1:2029672:C/A:-1.31298871130864 0.727940863740118\ chr1:2228889:C/G:-1.570481759004 0.765170049967457\ chr10:100000465:C/T:-0.701703282910107 0.629368417133545\ chr1:2306256:C/T:-1.72965371800758 0.786695642291442\
预期输出:文件 2 中的匹配行,格式与文件 2 相同(见上文)
chr10:10000003:C/T:-0.572267893158369 0.607055146639116\ chr10:100000202:C/A: 0.808838763489275 0.362093542746135\ chr10:100000465:C/T:-0.701703282910107 0.629368417133545\
到目前为止尝试过的命令:
awk -F":\r" 'NR==FNR{a[$1$2]++;next}{if(a 中的 $1$2){print}}' file1.txt file2.txt > output.txt
awk -F":\r" 'NR==FNR{a[($1$2)]++;next}{if(($1$2) in a){print}}' file1.txt file2.txt > 输出。 TXT
观察到的错误是空白输出.txt
你能帮我指出错误吗?
提前致谢!
答案1
您可以使用以下命令如有任何疑问请告诉我
awk 'NR==FNR{a[$1];next}($1 in a){print $0}' file1 file2
答案2
这才是你真正需要的:
$ awk -F':' 'NR==FNR{a[$1,$2]; next} ($1,$2) in a' file1 file2
chr10:10000003:C/T: -0.572267893158369 0.607055146639116\
chr10:100000202:C/A: 0.808838763489275 0.362093542746135\
chr10:100000465:C/T: -0.701703282910107 0.629368417133545\