首页 > 解决方案 > awk:打印与文件中的模式不匹配的行,查看特定列

问题描述

我有一个idFile

1006006
1006008
1006011
1007002
......   

famFile

1006 1006001 1006016 1006017 1
1006 1006006 1006016 1006017 1
1006 1006007 0       0       2
1006 1006008 1006007 1006006 2
1006 1006010 1006016 1006017 2
1006 1006011 1006016 1006017 1
1006 1006016 0       0       2
1006 1006017 0       0       1
1007 1007001 1007950 1007015 2
1007 1007002 1007014 1007015 2
......

我需要 grepfamFile中第二列idFile.

这个命令:

awk 'BEGIN { while(getline <"idFile") id[$0]=1; }
id[$2] ' famFile

返回所有匹配项:

1006 1006006 1006016 1006017 1
1006 1006008 1006007 1006006 2
1006 1006011 1006016 1006017 1
1007 1007002 1007014 1007015 2
......

但是我怎样才能修改命令来获得匹配的补码呢?

标签: awkpattern-matchingbioinformaticstext-processing

解决方案


$ awk 'NR==FNR{a[$1];next} !($2 in a)' idFile famFile
1006 1006001 1006016 1006017 1
1006 1006007 0       0       2
1006 1006010 1006016 1006017 2
1006 1006016 0       0       2
1006 1006017 0       0       1
1007 1007001 1007950 1007015 2

解释:

$ awk '
NR==FNR {                  # process the idFile
    a[$1]                  # hash to a 
    next                   # next id
}
!($2 in a)                 # if the second field id is not in a, output record
' idFile famFile           # mind the file order

推荐阅读