What is the difference between NR and FNR?

In awk, NR and FNR are both built-in variables, but they serve different purposes:

  • NR (Number of Records): This variable counts the total number of input records (lines) read so far across all input files. It increments with each line processed, regardless of how many files are being processed.

  • FNR (File Number of Records): This variable counts the number of records (lines) read from the current input file only. It resets to 1 when a new file is started.

In summary, NR is cumulative across all files, while FNR is specific to the current file being processed. This distinction is useful when working with multiple input files.

0 Comments

no data
Be the first to share your comment!