我只想保留 results.txt
中与 uniq.txt
中的 ID 匹配的行,基于 results.txt< 的第 3 列中的匹配项
。通常我会使用 grep -f uniq.txt results.txt
,但这并没有指定第 3 列。
uniq.txt
9606
234831
131
31313
结果.txt
readID seqID taxID score 2ndBestScore hitLength queryLength numMatches
A00260:70:HJM2YDSXX:4:1111:15519:16720 NC_000011.10 9606 169 0 28 151 1
A00260:70:HJM2YDSXX:3:1536:9805:14841 NW_021160017.1 9606 81 0 24 151 1
A00260:70:HJM2YDSXX:3:1366:27181:24330 NC_014803.1 234831 121 121 26 151 3
A00260:70:HJM2YDSXX:3:1366:27181:24330 NC_014973.1 443143 121 121 26 151 3
最佳答案
使用您展示的示例,请尝试以下代码。
awk 'FNR==NR{arr[$0];next} ($3 in arr)' uniq.txt results.txt
解释:
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition which will be TRUE when uniq.txt is being read.
arr[$0] ##Creating arrar with index of current line.
next ##next will skip all further statements from here.
}
($3 in arr) ##If 3rd field is present in arr then print line from results.txt here.
' uniq.txt results.txt ##Mentioning Input_file names here.
第二个解决方案: 如果您的字段编号未在 results.txt 中设置并且您想搜索整行的值,请尝试以下操作。
awk 'FNR==NR{arr[$0];next} {for(key in arr){if(index($0,key)){print;next}}}' uniq.txt results.txt
关于awk - 匹配特定列的 grep 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68300920/