Unix Manuals

awk reference

This is not a tutorial or full description of awk - it is a quick reference to the most commonly used built in functions and variables. We have ignored some less-used routines, such as the mathematical functions. This reference is based around the GNU awk implementation, as it is now very widely available.

NOTICE - New forum for all UNIX learners, users and programmers - click here

AWK built-in function reference

break Leave the current 'while' or 'for' loop.
close(filename expression or command expression) Close the file which was opened by the specified expression.
continue Start the next iteration of the current 'while' or 'for' loop.
delete(array[element] Delete the specified element of the array.
do
statements
while (expressions)
Execute statements, then evaluate expression. Repeat until expression evaluates false.
exit Execute the END instructions, then exit the program.
for (expr1; expr2; expr3)
statements
expr1 is evaluated - this is usually use to set up the conditions for the loop. If expr2 evaluates true, the statements are run. expr3 is then run - this is often used to increment a counter. The construct then loops, checking expr2 again. When expr2 evaluates false, the loop terminates.
function fname(parameters) {
statements
}
Define a function.
getline [varname] [<filename] Get a line from the specified file. If varname is not specified, the line is loaded into $0. If varname is specified, the variable is populated instead of $0. See also additional syntax below.
statements | getline [varname] The statements are run, and the output is passed to getline. varname operates as described above.
gsub (regex, newstring, string) Replace every match of the regular expression regex in string with newstring.
if (condition)
statements
[else
statements]
If condition evaluates true, the first block of statements are run. If not, the statements in the optional 'else' section are run.
index(substring, string) Returns the index of substring within string, or 0 if not present.
length([string]) Returns the length of string, or the length of $0 if string not specified.
match(string, regex) Returns the position of the first match for the regular expression regex in string, or 0 if no matches are found. Sets RSTART and RLENGTH variables.
next Ignore any further instructions for this record. Read the next record, and process it.
print [expressions] [>destination] Print the expressions (or the current line if no expression given) to the specified destination (the standard output, if not specified).
printf format expressions [>destination] Printf the expressions formatted according to format to the specified destination, or standard output if none specified.
return [expression] Return from a function, returning the value of expression to the caller.
split(string, array [,regex] Split the string into components, populating the array with the results. Use the regular expression regex to specify the split boundary, or the contents of the variable FS is regex if not specified.
sprintf formai expressions As printf, except that the formatted string is returned instead of printed.
sub (regex, newstring, string) As sub, except only the first match is changed.
substr(string, start [,length] Return length characters from the specified string, starting from start. If length is not specified, return rest of record.
system(command) Execute the command, and return the exit status.
tolower(string) Return the string with all upper case characters replaced with their lower case equivalents.
toupper(string) Return the string with all lower case characters replaced with their upper case equivalents.
while(condition)
statements
While the specified condition is true, execute the statements. Re-evaluate the condition before each execution of the statements.

AWK built-in variable reference

ARGC Number of command-line arguments.
ARGIND Index (within ARGV) of file currently being processed.
ARGV Array of command-line arguments.
CONVFMT Conversion format for numbers in 'printf' syntax.
ENVIRON Array containing values of current environment.
ERRNO String containing text version of most recent error.
FIELDWIDTHS List of field widths separated by white spaces. If specified, is used by GNU awk instead of FS to parse records into fields.
FILENAME Name of input file.
FNR Record number in input file.
FS Field separator.
IGNORECASE If non-zero, many functions will ignore case when doing comparisons.
NF Number of fields in current record.
NR Number of records processed.
OFMT Output format for numbers in 'printf' syntax.
OFS Output field separator.
ORS Output record separator.
RS Input record separator.
RT Record terminator.
RSTART Index of first character matched by a successful call to the match() function.
RLENGTH Length of string matched by a successful call to the match() function.
SUBSET Character used to separate multiple items in arrays.

Home


© Copyright 2000-2001, Tom Reader, All Rights Reserved.
UNIX is a registered trademark of The Open Group in the US and other countries.
For more information on the use of the UNIX trademark, click here.
The contents of this site are not connected with or endorsed by The Open Group in any way.