在 awk 中查找 File2 中存在的 File1 中的列

Finding a Column in File1 present in File2 in awk

我有 2 个文件如下

File1
USA,China,India,Canada

File2
Iran|XXXXXX|Iranian
Iraq|YYYYYY|Iraquian
Saudi|ZZZZZ|Saudi is a Rich Country
USA|AAAAAA|USA is United States of America.
India|IIII|India got freedom from British.
Scot|SSSSS|Canada Mexio.

如何使用 awk 读取 File1 中的值并检查它是否与 File2 中的第一个分隔字符串匹配?我试过这个但我无法实现它。请帮忙

对于上述输入,输出应该是

USA Matches
China Not Matched
India Matches
Canada Not Matches

能否请您尝试以下。

awk 'FNR==NR{a[];next} {for(i=1;i<=NF;i++){if($i in a){print $i,"Matches"} else {print $i,"Not Matches."}}}' FS="|" Input_file2 FS="," Input_file1

你也可以试试 Perl

$ cat vinoth1
USA,China,India,Canada
$ cat vinoth2
Iran|XXXXXX|Iranian
Iraq|YYYYYY|Iraquian
Saudi|ZZZZZ|Saudi is a Rich Country
USA|AAAAAA|USA is United States of America.
India|IIII|India got freedom from British.
Scot|SSSSS|Canada Mexio.
$ perl -F, -lane ' BEGIN { $x=qx(cat vinoth2) } print $_,$x=~/^$_/m ? " matches" : " not matches" for(@F) ' vinoth1
USA matches
China not matches
India matches
Canada not matches