”;
AWK provides several built-in variables. They play an important role while writing AWK scripts. This chapter demonstrates the usage of built-in variables.
Standard AWK variables
The standard AWK variables are discussed below.
ARGC
It implies the number of arguments provided at the command line.
Example
[jerry]$ awk ''BEGIN {print "Arguments =", ARGC}'' One Two Three Four
On executing this code, you get the following result −
Output
Arguments = 5
But why AWK shows 5 when you passed only 4 arguments? Just check the following example to clear your doubt.
ARGV
It is an array that stores the command-line arguments. The array”s valid index ranges from 0 to ARGC-1.
Example
[jerry]$ awk ''BEGIN { for (i = 0; i < ARGC - 1; ++i) { printf "ARGV[%d] = %sn", i, ARGV[i] } }'' one two three four
On executing this code, you get the following result −
Output
ARGV[0] = awk ARGV[1] = one ARGV[2] = two ARGV[3] = three
CONVFMT
It represents the conversion format for numbers. Its default value is %.6g.
Example
[jerry]$ awk ''BEGIN { print "Conversion Format =", CONVFMT }''
On executing this code, you get the following result −
Output
Conversion Format = %.6g
ENVIRON
It is an associative array of environment variables.
Example
[jerry]$ awk ''BEGIN { print ENVIRON["USER"] }''
On executing this code, you get the following result −
Output
jerry
To find names of other environment variables, use env command.
FILENAME
It represents the current file name.
Example
[jerry]$ awk ''END {print FILENAME}'' marks.txt
On executing this code, you get the following result −
Output
marks.txt
Please note that FILENAME is undefined in the BEGIN block.
FS
It represents the (input) field separator and its default value is space. You can also change this by using -F command line option.
Example
[jerry]$ awk ''BEGIN {print "FS = " FS}'' | cat -vte
On executing this code, you get the following result −
Output
FS = $
NF
It represents the number of fields in the current record. For instance, the following example prints only those lines that contain more than two fields.
Example
[jerry]$ echo -e "One TwonOne Two ThreenOne Two Three Four" | awk ''NF > 2''
On executing this code, you get the following result −
Output
One Two Three One Two Three Four
NR
It represents the number of the current record. For instance, the following example prints the record if the current record number is less than three.
Example
[jerry]$ echo -e "One TwonOne Two ThreenOne Two Three Four" | awk ''NR < 3''
On executing this code, you get the following result −
Output
One Two One Two Three
FNR
It is similar to NR, but relative to the current file. It is useful when AWK is operating on multiple files. Value of FNR resets with new file.
OFMT
It represents the output format number and its default value is %.6g.
Example
[jerry]$ awk ''BEGIN {print "OFMT = " OFMT}''
On executing this code, you get the following result −
Output
OFMT = %.6g
OFS
It represents the output field separator and its default value is space.
Example
[jerry]$ awk ''BEGIN {print "OFS = " OFS}'' | cat -vte
On executing this code, you get the following result −
Output
OFS = $
ORS
It represents the output record separator and its default value is newline.
Example
[jerry]$ awk ''BEGIN {print "ORS = " ORS}'' | cat -vte
On executing the above code, you get the following result −
Output
ORS = $ $
RLENGTH
It represents the length of the string matched by match function. AWK”s match function searches for a given string in the input-string.
Example
[jerry]$ awk ''BEGIN { if (match("One Two Three", "re")) { print RLENGTH } }''
On executing this code, you get the following result −
Output
2
RS
It represents (input) record separator and its default value is newline.
Example
[jerry]$ awk ''BEGIN {print "RS = " RS}'' | cat -vte
On executing this code, you get the following result −
Output
RS = $ $
RSTART
It represents the first position in the string matched by match function.
Example
[jerry]$ awk ''BEGIN { if (match("One Two Three", "Thre")) { print RSTART } }''
On executing this code, you get the following result −
Output
9
SUBSEP
It represents the separator character for array subscripts and its default value is