Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What does the awk nr variable in awk mean?


Asked by Colten Cline on Nov 29, 2021 FAQ



Awk NR gives you the total number of records being processed or line number. In the following awk NR example, NR variable has line number, in the END section awk NR tells you the total number of records in a file.
Subsequently,
Awk FNR Example: Number of Records relative to the current input file. When awk reads from the multiple input file, awk NR variable will give the total number of records relative to all the input file. Awk FNR will give you number of records for each input file.
Next, NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file.
Furthermore,
FILENAME variable gives the name of the file being read. Awk can accept number of input files to process. $ awk ' {print FILENAME}' student-marks student-marks student-marks student-marks student-marks student-marks In the above example, it prints the FILENAME i.e student-marks for each record of the input file.
Additionally,
Summary of AWK Commands AWK Built-in Variables FS - The Input Field Separator Variable OFS - The Output Field Separator Variable NF - The Number of Fields Variable NR - The Number of Records Variable RS - The Record Separator Variable ORS - The Output Record Separator Variable FILENAME - The Current Filename Variable Associative Arrays