匹配特定列的grep文件
我想仅保留线results.txt匹配的ID的uniq.txt基础上在第3栏的比赛results.txt。通常我会使用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