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 $0=line1 f2 f3 $1=line1 $2=f2 $3=f3
Line no:2,No of fields:3 $0=line2 f4 f5 $1=line2 $2=f4 $3=f5
Line no:3,No of fields:3 $0=line3 f6 f7 $1=line3 $2=f6 $3=f7
We can print the last field of a line as print $NF, the next to last as $(NF-1), and so on.
awk also supports a printf() function with the same syntax as in C.
The following command prints the second and third field of every line:
$awk '{ print $3,$2 }' file
We can use NR to count the number of lines in a file:
$ awk 'END{ print NR }' file
Here, we only use the END block. Awk updates NR as each line is read. When awk reaches the end of the file, NR will contain the last line number. You can sum up all the numbers from each line of field 1 as follows:
$ seq 5 | awk 'BEGIN{ sum=0; print "Summation:" }
{ print $1"+"; sum+=$1 } END { print "=="; print sum }'
Summation:
1+
2+
3+
4+
5+
==
15