NR and NF

NR and NF are set each time a new record is read.

AWK sets the variable NR whenever a new record from input file is read. It's value represent the number of current input record being processed by AWK. It is generally used to number the records in a file. For example, to print the number of records with each record in the output from the emp.dat file, we can use it as follows:

$ awk '{ print NR "." $0 }' emp.dat

The output of the execution of the preceding code is as follows:

1.Jack    Singh   9857532312  jack@gmail.com      M   hr      20002.Jane    Kaur    9837432312  jane@gmail.com      F   hr      18003.Eva     Chabra  8827232115  eva@gmail.com       F   lgs     21004.Amit    Sharma  9911887766  amit@yahoo.com      M   lgs     23505.Julie   Kapur   8826234556  julie@yahoo.com     F   Ops     25006.Ana Khanna ...

Get Learning AWK Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.