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

Numbering and calculations with AWK

In this section, we look at the working of one-liners for the numbering of lines in a file, and performing calculations on fields and line numbers:

  • Numbering lines in multiple files separately: The following example uses FNR – the file line number AWK built-in variable before each line to print the current line number for each file, separately. If we use this one-liner with 2 files, named cars.dat containing 12 lines and the second file, label.dat containing 7 lines, it will print the first line number from 1 to 12, and then 1 to 7 for 2 files, respectively. The FNR value is reset for each file:
$ awk '{ print FNR "\t" $0 }' cars.dat label.dat
  • Numbering all lines for multiple files together: In this ...

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