AWK FNR == NR in an empty file

I run the following command, which works fine while their contents are in the first file:

awk -F, 'FNR==NR {a[tolower($1)]++; next} !a[tolower($1)]' OutSideSyncUsers.csv NewUsers.csv

If the first file is empty, the command does not work. I found this link online:

all programs using the idiom of two files will not work correctly if the first file is empty (in this case awk will perform the actions associated with NR == FNR when reading the second file). To fix this, you can strengthen the condition NR == FNR by adding a test that checks that also FILENAME is equal to ARGV [1].

I am not sure that I understand how (or where) in the request to add a test check equal to ARGV [1]

Any help would be apperciated

+4
source share
2 answers

You can use the following additional conditions:

awk -F, 'ARGV[1] == FILENAME{a[tolower($1)]++; next} !a[tolower($1)]' OutSideSyncUsers.csv NewUsers.csv

ARGV[1]==FILENAME , . , . , , !a[tolower($1)] .

+4

GNU awk ARGIND:

awk -F, '{k=tolower($1)} ARGIND==1{a[k]; next} !(k in a)' OutSideSyncUsers.csv NewUsers.csv

awks :

awk -F, '{k=tolower($1)} FILENAME==ARGV[1]{a[k]; next} !(k in a)' OutSideSyncUsers.csv NewUsers.csv

, / .

+4

All Articles