awk在2个文件中找到不匹配

时间:2016-03-16 17:42:01

标签: awk

我正在尝试查找file1中未找到的file2下方的行。下面的awk会运行,但不会产生任何结果。谢谢你:)。

文件1

955763
957852
976270

file2的

chr1    955543  955763  chr1:955543-955763  AGRN-6|gc=75
chr1    957571  957852  chr1:957571-957852  AGRN-7|gc=61.2
chr1    970621  970740  chr1:970621-970740  AGRN-8|gc=57.1

所需的输出

2 ids found
976270 missing

awk (missing.awk)

BEGIN { FS="[[:space:]]+|-" }
NR == FNR { seen[$0]; next }
$3 in seen { found[$3]; delete seen[$6] }
END { print length(found) " ids found"
  for (i in seen) print i " missing" }

awk -f missing.awk file1 file2

1 个答案:

答案 0 :(得分:2)

awk救援!

如果file2大小比file1大得多,这种方法更好

awk 'NR==FNR{a[$1];next}
     $3 in a{c++; delete a[$3]}
         END{if(c) print c " ids found"; 
             for(k in a) print k " missing"}' file1 file2

2 ids found
976270 missing