I hope this example will help everyone to understand how awk internal variables like NR, FNR etc change when awk is processing two files.
awk '{print "NR:",NR,"FNR:",FNR,"fname:",FILENAME,"Field1:",$1}' file1 file2
NR: 1 FNR: 1 fname: file1 Field1: f1d1
NR: 2 FNR: 2 fname: file1 Field1: f1d5
NR: 3 FNR: 3 fname: file1 Field1: f1d9
NR: 4 FNR: 1 fname: file2 Field1: f2d1
NR: 5 FNR: 2 fname: file2 Field1: f2d5
NR: 6 FNR: 3 fname: file2 Field1: f2d9
Where file1 and file2 look like:
$ cat file1
f1d1 f1d2 f1d3 f1d4
$ cat file2
f2d1 f2d2 f2d3 f2d4
Notice how NR
value keeps increasing among all files, while FNR
resets on each file.
This is why the expression NR==FNR
always refer to the first file fed to awk, since only in first file is possible to have NR
equal to FNR
.