O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

FNR

The FNR is set each time a new record is read. It useful when dealing with multiple input files. It provides the access to the number of current input records relevant to the current input file. Whereas NR continues to grow between multiple files, it continues to incrementally grow from the last NR number value of the previous file until the last record of the last file is processed.

In the following example, we print the NR and FNR values while processing two files consecutively, as follows:

$ vi  fnr.awkBEGIN    {        print "Example to show both NR and FNR difference..!"    }{    printf "FILENAME=%s  NR=%s  FNR=%s\n", FILENAME, NR, FNR;}END    {    printf "END Block: NR=%s FNR=%s\n", NR, FNR
}$ awk -f fnr.awk label.dat cars.dat

The output of the execution ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required