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.