Some special variables that can be used with awk are as follows:
- NR: This stands for the current record number, which corresponds to the current line number when awk uses lines as records.
- NF: This stands for the number of fields, and corresponds to the number of fields in the current record being processed. The default field delimiter is a space.
- $0: This is a variable that contains the text of the current record.
- $1: This is a variable that holds the text of the first field.
- $2: This is a variable that holds the text of the second field.
Consider this example:
$ echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | \
awk '{
print "Line no:"NR",No of fields:"NF, "$0="$0,
"$1="$1,"$2="$2,"$3="$3
}'
Line no:1,No of fields:3 ...