Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK FNR==NR on an Empty File

Tags:

bash

awk

I am running the following command which works great as long as their is content 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 doesnt work. I found online this reference:

all the programs that use the two-files idiom will not work correctly if the first file is empty (in that case, awk will execute the actions associated to NR==FNR while reading the second file). To correct that, you can reinforce the NR==FNR condition by adding a test that checks that also FILENAME equals ARGV[1].

Im not sure I understand how (or where) in the query to add the test check equals ARGV[1]

Any help would be apperciated

like image 327
moore1emu Avatar asked May 03 '16 19:05

moore1emu


2 Answers

You can use this additional conditions like this:

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

Additional condition ARGV[1]==FILENAME ensures that first block only executes for the first file only in argument list. When first file is empty then it will just skip the first block. This way you make sure 2nd block !a[tolower($1)] is always executing on 2nd file in argument list.

like image 184
anubhava Avatar answered Sep 21 '22 20:09

anubhava


With GNU awk use ARGIND:

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

with other awks an approximation is:

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

but that fails if/when you are parsing the same file twice.

like image 38
Ed Morton Avatar answered Sep 25 '22 20:09

Ed Morton