Home » Linux » What is “NR==FNR” in awk?

What is “NR==FNR” in awk?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am learning file comparison using awk.

I found syntax like below,

awk 'NR==FNR{a[$1];next}$1 in a{print $1}' file1 file2

I couldn’t understand what is significance of NR==FNR in this?
If I try with FNR==NR then also I get same output?

What exactly it does ?

Answers:

Look for keys (first word of line) in file2 that are also in file1.
Step 1: fill array a with the first words of file 1:

awk '{a[$1];}' file1

Step 2: Fill array a and ignore file 2 in the same command. For this check the total number of records until now with the number of the current input file.

awk 'NR==FNR{a[$1]}' file1 file2

Step 3: Ignore actions that might come after } when parsing file 1

awk 'NR==FNR{a[$1];next}' file1 file2 

Step 4: print key of file2 when found in the array a

awk 'NR==FNR{a[$1];next} $1 in a{print $1}' file1 file2

Questions:
Answers:

In awk, FNR refers to the record number (typically the line number) in the current file and NR refers to the total record number. This means that the condition NR==FNR is only true for the first file, as FNR resets back to 1 for the first line of each file but NR keeps on increasing.

This pattern is typically used to perform actions on only the first file. The next inside the block means any further commands are skipped, so they are only run on files other than the first.

It’s unclear why you would expect FNR==NR to be any different to NR==FNR.

Questions:
Answers:

Look up NR and FNR in the awk manual and then ask yourself what is the condition under which NR==FNR in the following example:

$ cat file1
a
b
c

$ cat file2
d
e

$ awk '{print FILENAME, NR, FNR, $0}' file1 file2
file1 1 1 a
file1 2 2 b
file1 3 3 c
file2 4 1 d
file2 5 2 e

Questions:
Answers:

There are awk built-in variables.

NR – It gives the total number of records processed.

FNR – It gives the total number of records for each input file.