使用部分匹配和打印消息来匹配行

时间:2017-04-10 18:12:50

标签: awk

我正在尝试使用下面的awkfile1中的行与file2中的行匹配,使用部分匹配。如果字符串与另一个字符串部分匹配,则会为该行打印自定义输出(.... missing but ... found)。

目前,要搜索的名称显示缺失,因为它不完全匹配。谢谢你:)。

文件1

ID
NAME
MRE11

file2的

NAME
ID
MRE11A

所需的输出

2 ids found
MRE11 missing but MRE11A found

AWK

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

也许:

else print (i in seen) "missing" but i found }

1 个答案:

答案 0 :(得分:1)

这样的事情?

$ awk 'NR==FNR {a[$1]; next} 
               {if($1 in a) c++; 
                else for(k in a) 
                        if(k~$1) {print $1,"missing but found",k; break}} 
       END     {print c,"ids matched"}' file2 file1

MRE11 missing but found MRE11A
2 ids matched

要更改顺序,您需要将匹配的数据保留在数组中并在END块中打印。但是,这似乎更容易。