This is an old revision of the document!
Table of Contents
AWK - AWK Variables
AWK provides several built-in variables.
They play an important role while writing AWK scripts.
Here is a list of some of the built-in variables supported by AWK:
Variable | Details |
---|---|
ARGC | The number of command line arguments. |
ARGIND | The index in ARGV of the current file being processed. |
ARGV | Array of command line arguments. The array is indexed from 0 to ARGC - 1. |
BINMODE | Specifies use of “binary” mode for all file I/O. |
CONVFMT | The conversion format for numbers, “%.6g”, by default. |
FIELDWIDTHS | A white-space separated list of field widths. When set the input is parsed into fields of fixed width, instead of using the value of the FS variable as the field separator. |
FNR | The input record number in the current input file. |
FPAT | A regular expression describing the contents of the fields in a record. When set the input is parsed into fields, where the fields match the regular expression, instead of using the value of the FS variable as the field separator. |
FS | The input field separator, a space by default. |
IGNORECASE | Controls the case-sensitivity of all regular expression and string operations. |
NF | The number of fields in the current input record. |
NR | The total number of input records seen so far. |
OFMT | The output format for numbers, “%.6g”, by default. |
OFS | The output field separator, a space by default. |
ORS | The output record separator, by default a newline. |
RS | The input record separator, by default a newline. |
RT | The record terminator. |
SUBSEP | The character used to separate multiple subscripts in array elements, by default “\034”. |
ARGC
It implies the number of arguments provided at the command line.
awk 'BEGIN {print "Arguments =", ARGC}' One Two Three Four
returns:
Arguments = 5
NOTE: The number of arguments reported is one more than the variables passed; as this also includes the calling program as the first argument.
ARGV
It is an array that stores the command-line arguments.
The array's valid index ranges from 0 to ARGC-1.
awk 'BEGIN { for (i = 0; i < ARGC - 1; ++i) { printf "ARGV[%d] = %s\n", i, ARGV[i] } }' one two three four
returns:
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.
awk 'BEGIN { print "Conversion Format =", CONVFMT }'
returns:
Conversion Format = %.6g
ENVIRON
It is an associative array of environment variables.
awk 'BEGIN { print ENVIRON["USER"] }'
returns:
peter
NOTE: To find names of other environment variables, use the env command.
FILENAME
It represents the current file name.
awk 'END {print FILENAME}' test.txt
returns:
test.txt
NOTE: 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.
awk 'BEGIN {print "FS = " FS}' | cat -vte
returns:
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.
echo -e "One Two\nOne Two Three\nOne Two Three Four" | awk 'NF > 2'
returns:
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.
echo -e "One Two\nOne Two Three\nOne Two Three Four" | awk 'NR < 3'
returns:
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 a new file.
OFMT
It represents the output format number and its default value is %.6g.
awk 'BEGIN {print "OFMT = " OFMT}'
returns:
OFMT = %.6g
OFS
It represents the output field separator and its default value is space.
awk 'BEGIN {print "OFS = " OFS}' | cat -vte
returns:
OFS = $
ORS
It represents the output record separator and its default value is newline.
awk 'BEGIN {print "ORS = " ORS}' | cat -vte
returns:
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.
awk 'BEGIN { if (match("One Two Three", "re")) { print RLENGTH } }'
returns:
2
RS
It represents (input) record separator and its default value is newline.
awk 'BEGIN {print "RS = " RS}' | cat -vte
returns:
RS = $ $
RSTART
It represents the first position in the string matched by match function.
awk 'BEGIN { if (match("One Two Three", "Thre")) { print RSTART } }'
returns:
9
SUBSEP
It represents the separator character for array subscripts and its default value is \034.
awk 'BEGIN { print "SUBSEP = " SUBSEP }' | cat -vte
returns:
SUBSEP = ^\$
$0
It represents the entire input record.
awk '{print $0}' test.txt
returns:
10 Peter Terence Roux 45 11 Virginia Genevieve Roux 45 12 Felix Devon Roux 5 13 David Bruce Stevenson 48 14 Bob James Smith 16 48 Adam Winter Ridley 23
$n
It represents the nth field in the current record where the fields are separated by FS.
awk '{print $2 "\t" $5}' test.txt
returns:
Peter 45 Virginia 45 Felix 5 David 48 Bob 16 Adam 23