Built-in Variables
Like many programming languages, awk has special variables that are available all the time. Following is a list of some common and useful ones.
Since awk deals with records and fields, there are built-in variables for them. Here are some of them:
- FS (Field Separator)
The character or regex that is used to separate fields. Default is a space.
- RS (Record Separator)
The character that separates records. By default the newline character, but can be reset using this variable.
- NF (Number of Fields)Number of fields in current record.
Example: awk 'BEGIN {FS=":"}; {print NF }' names.txt
- OFS (Output Field Separator)
Single space is the default. Any string of characters can be used. Usually used with a print statement.
- ORS (Output Record Separator)
Default is a newline character '\n' and is output at the end of every print statement. Can be changed.
- FNR (File Number of Records)
Number of records (lines) that have been read from current input file.
Example: awk 'END {print FNR}' names.txt
- NR (Number of Records)Total number of records (lines) read so far from all input files.
Example:awk 'END {print NR}' names.txt
Visit the Resources page to find more about awk.