5

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

moore1emu
  • 476
  • 8
  • 27

2 Answers2

6

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
6

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 so then you can set variables between the files:

awk -F, '{k=tolower($1)} !f{a[k]; next} !(k in a)' OutSideSyncUsers.csv f=1 NewUsers.csv
Ed Morton
  • 188,023
  • 17
  • 78
  • 185