awk - 匹配特定列的 grep 文件

标签 awk grep

我只想保留 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/

相关文章:

Linux,打印文件中的所有行,而不是以

linux - shell 脚本中的 for x in {1..10} 仅运行一次

shell - awk 不检查语句是否正确它在输出中显示所有打印行

bash - 使用awk可被5整除的列跳过行的平均值

regex - 在同一行中匹配模式 n 次的行

regex - Grep:省略搜索和结果之间的空格

bash - 使用 sed/awk 将变量的内容打印到输出文件中的特定行

awk - gsub : remove till first occurence instead of last occurence of a given character in a line

linux - 如何从使用 `grep -P` 转换为 `grep -E` ?

bash - 用于搜索必须包含两个特定关键字的文本文件的 Unix 脚本