User Tools

Site Tools


awk:awk_variables

This is an old revision of the document!


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:

VariableDetails
ARGCThe number of command line arguments.
ARGINDThe index in ARGV of the current file being processed.
ARGVArray of command line arguments. The array is indexed from 0 to ARGC - 1.
BINMODESpecifies use of “binary” mode for all file I/O.
CONVFMTThe conversion format for numbers, “%.6g”, by default.
FIELDWIDTHSA 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.
FNRThe input record number in the current input file.
FPATA 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.
FSThe input field separator, a space by default.
IGNORECASEControls the case-sensitivity of all regular expression and string operations.
NFThe number of fields in the current input record.
NRThe total number of input records seen so far.
OFMTThe output format for numbers, “%.6g”, by default.
OFSThe output field separator, a space by default.
ORSThe output record separator, by default a newline.
RSThe input record separator, by default a newline.
RTThe record terminator.
SUBSEPThe 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

awk/awk_variables.1588786403.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki