0

I've the following code:

awk -F\; 'FNR==NR{a[$3]=$3;a[$9]=$9;a[$11]=$11;next}(a[$3]!=""){print $3, $9, $11, a[$3], a[$9], a[$11]}' $fileB $fileA |awk '($4!="" && $5=="" && $6==""){print $1}' > temp

The awk command receive 2 files in input. I want to understand the checks are made when comparing files.

Thanks

FabioG
  • 1
  • 1
    To understand the `FNR==NR` thingy, you can start giving a read to [Idiomatic awk](http://backreference.org/2010/02/10/idiomatic-awk/). – fedorqui May 17 '16 at 09:28
  • 1
    read this answer: http://stackoverflow.com/questions/14984340/using-awk-to-process-input-from-multiple-files/14984673#14984673 – Kent May 17 '16 at 09:34

1 Answers1

1

If you're just asking about how the script figures out which file it's processing, that's the FNR==NR bit.

The FNR variable is the record number for the current file being processed, it resets back to one when you start processing a new file.

The NR variable is the record number for the set of all files being processed, it does not reset when moving to a new file.

So FNR and NR will only be equal for the first file being processed. Let's say you're processing three files, each a hundred lines in length. The values for NR and FNR are as follows:

File #        NR       FNR
------   -------   -------
File 1     1-100     1-100
File 2   101-200     1-100
File 3   201-300     1-100

So, with the first awk script you have in the question, the first file is used solely to populate the a array. The use of next guarantees no other code will run for lines in that file.

The second file will use values in certain columns to output columns along with relevant values from the array created previously.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953