不区分大小写的 awk vlookup

case insensitive awk vlookup

我正在尝试使用 awk 对文件中的第一列进行不区分大小写的 vlookup,但它忽略了 IGNORECASE 并且不匹配小写字母也使用相同的 awk 如何比较第三列

awk -F, 'BEGIN{IGNORECASE=1} FNR==NR{a[]=",";next} {print (( in a) ? [=10=]","a[] : [=10=]",NA,NA");}'

cat f1.txt
FOO,LONDON,45,789
goo,US,46,9876

cat f2.txt
FOO,LONDON,45,789
GOO,US,46,9876


awk -F, 'BEGIN{IGNORECASE=1} FNR==NR{a[]=",";next} {print (( in a) ? [=10=]","a[] : [=10=]",NA,NA");}' f1.txt f2.txt

FOO,LONDON,45,789,45,789
GOO,US,46,9876,NA,NA

请参阅 GNU Awk 页的以下摘录,

Another method, specific to gawk, is to set the variable IGNORECASE to a nonzero value (see Built-in Variables). When IGNORECASE is not zero, all regexp and string operations ignore case.

In general, you cannot use IGNORECASE to make certain rules case insensitive and other rules case sensitive, as there is no straightforward way to set IGNORECASE just for the pattern of a particular rule.17 To do this, use either bracket expressions or tolower(). However, one thing you can do with IGNORECASE only is dynamically turn case sensitivity on or off for all the rules at once.

所以使用 GNU Awk's tolower() 函数作为输入,

awk -F, 'FNR==NR{a[tolower()]=",";next} {print ((tolower() in a) ? [=10=]","a[tolower()] : [=10=]",NA,NA");}' f1.txt f2.txt
FOO,LONDON,45,789,45,789
GOO,US,46,9876,46,9876