AWK Built-in Variables - AWK

What are AWK built in variables?

AWK facilitates many built-in variables. They play a major role in writing AWK scripts. This chapter illustrates the use of built-in variables.

What are Standard AWK variables?

The standard AWK variables are discussed in the following section.

ARGC

It signifies the number of arguments facilitated at the command line.

Example

Upon executing this code, you get the below output −

Output

But why AWK shows 5 if you passed only 4 arguments? Just see the below example to get clarified.

ARGV

It is an array that will store the command-line arguments. Valid index of the arrays ranges from 0 to ARGC-1.

Example

Upon executing this code, you get the below output −

Output

CONVFMT

It indicates the conversion format for numbers. Default value is %.6g.

Example

Upon executing this code, you get the below output −

Output

ENVIRON

It is a related array of environment variables.

Example

Upon executing this code, you get the below output −

Output

To figure out the names of other environment variables, you can use env command.

FILENAME

It indicates the current file name.

Example

Upon executing this code, you get the below output −

Output

Please observe that FILENAME is undefined in the BEGIN block.

FS

It indicates the (input) field separator. Default value of FS is space. You may also alter this by employing -F command line option.

Example

Upon executing this code, you get the below output −

Output

NF

It indicates the number of fields in the present record. For example, the below example prints only those specific lines that consists of more than two fields.

Example

Upon executing this code, you will obtain the below output −

Output

NR

It indicates the number of the current record. For example, the below example will print the record if the present record consists of less than three fields.

Example

Upon executing this code, you get the below output −

Output

FNR

It is likely to NR, but relative to the present file. It is helpful when AWK is being operated on multiple files and the value of FNR resets with new file.

OFMT

It indicates the output format number. Default value is %.6g.

Example

Upon executing this code, you will obtain the below output −

Output

OFS

It indicates the output field separator. Default value is space.

Example

Upon executing this code, you will obtain the below output −

Output

ORS

It indicates the output record separator. Default value is newline.

Example

Upon executing the above code, you will obtain the below output −

Output

RLENGTH

It indicates the length of the string matches with the match function. AWK's match function will search for a given string in the input-string.

Example

Upon executing this code, you will obtain the below output −

Output

RS

It indicates (input) record separator. Default value is newline.

Example

Upon executing this code, you will obtain the below output −

Output

RSTART

It indicates the beginning position in the string that is matched by match function.

Example

Upon executing this code, you will obtain the below output −

Output

SUBSEP

It indicates the separator character for array subscripts. Default value is \034.

Example

Upon executing this code, you will obtain the below output −

Output

It indicates the complete input record.

Example

Upon executing this code, you will obtain the below output −

Output

$n

It indicates the nth field in the present record where the fields are separated by FS.

Example

Upon executing this code, you get the following result −

Output

GNU AWK Specific Variables

GNU AWK specific variables are −

ARGIND

It indicates the index in ARGV of the present file that is being processed.

Example

Upon executing this code, you will obtain the below output −

Output

BINMODE

It is employed to define binary mode for all file I/O on non-POSIX systems. Numeric values of 1, 2, or 3 define that input files, output files, or all files, proportionately, must employ binary I/O. String values of r or w define that input files or output files, proportionately, must employ binary I/O. String values of rw or wr define that all files must employ binary I/O.

ERRNO

A string represents an error when a redirection is failed for getline or if close call is failed.

Example

Upon executing this code, you will obtain the below output −

Output

FIELDWIDTHS

A space separated list of field widths variable is set, GAWK will parse the input into fields of fixed width, rather than employing the value of the FS variable as field separator.

IGNORECASE

When this variable is being set, GAWK will become case-insensitive. The below example explains this −

Example

Upon executing this code, you will obtain the below output −

Output

LINT

It facilitates dynamic control of the --lint option from GAWK program. When this variable is being set, GAWK will print lint warnings. When assigned with the string value fatal, lint warnings will become fatal errors, similarly like --lint=fatal.

Example

Upon executing this code, you will obtain the below output −

Output

PROCINFO

This is a relative array consisting of data about the process, like real and effective UID numbers, process ID number etc.

Example

Upon executing this code, you will obtain the below output −

Output

TEXTDOMAIN

It indicates the text domain of the AWK program. It is employed to figure out the localized translations for the program's strings.

Example

Upon executing this code, you will obtain the below output −

Output

The above output displays English text due to en_IN locale

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

AWK Topics