Using awk

Built-in variables

Besides reading the input and splitting it into fields, awk counts the number of records read and the number of fields within the current record; you can use these counts in your awk programs. The built-in variable NR is the number of the current record, and NF is the number of fields in the record. For example, the following program prints the number of each line and how many fields it has:

   { print NR, NF }
This program prints each record preceded by its record number:
   { print NR, $0 }
In addition to NR, awk supplies the built-in variables listed in ``awk internal variables''.

awk internal variables

Variable Meaning Default
ARGC number of command-line arguments -
ARGV array of command-line arguments -
FILENAME name of current input file -
FNR record number in current file -
FS input field separator space or tab
NF number of fields in current record -
NR number of records read so far -
OFMT output format for numbers %.6g
OFS output field separator space
ORS output record separator newline
RS input record separator newline
RSTART index of first character matched by match -
RLENGTH length of string matched by match -
SUBSEP subscript separator "\034"

Next topic: User-defined variables
Previous topic: Field variables

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003