Go to the first, previous, next, last section, table of contents.


Built-in Functions

awk provides a number of built-in functions for performing numeric operations, string related operations, and I/O related operations.

The built-in arithmetic functions are:

atan2(y, x)
the arctangent of y/x in radians.
cos(expr)
the cosine of expr, which is in radians.
exp(expr)
the exponential function (e ^ expr).
int(expr)
truncates to integer.
log(expr)
the natural logarithm of expr.
rand()
a random number between zero and one.
sin(expr)
the sine of expr, which is in radians.
sqrt(expr)
the square root function.
srand([expr])
use expr as a new seed for the random number generator. If no expr is provided, the time of day is used. The return value is the previous seed for the random number generator.

awk has the following built-in string functions:

gensub(regex, subst, how [, target])
If how is a string beginning with `g' or `G', then replace each match of regex in target with subst. Otherwise, replace the how'th occurrence. If target is not supplied, use $0. The return value is the changed string; the original target is not modified. Within subst, `\n', where n is a digit from one to nine, can be used to indicate the text that matched the n'th parenthesized subexpression. This function is gawk-specific.
gsub(regex, subst [, target])
for each substring matching the regular expression regex in the string target, substitute the string subst, and return the number of substitutions. If target is not supplied, use $0.
index(str, search)
returns the index of the string search in the string str, or zero if search is not present.
length([str])
returns the length of the string str. The length of $0 is returned if no argument is supplied.
match(str, regex)
returns the position in str where the regular expression regex occurs, or zero if regex is not present, and sets the values of RSTART and RLENGTH.
split(str, arr [, regex])
splits the string str into the array arr on the regular expression regex, and returns the number of elements. If regex is omitted, FS is used instead. regex can be the null string, causing each character to be placed into its own array element. The array arr is cleared first.
sprintf(fmt, expr-list)
prints expr-list according to fmt, and returns the resulting string.
sub(regex, subst [, target])
just like gsub, but only the first matching substring is replaced.
substr(str, index [, len])
returns the len-character substring of str starting at index. If len is omitted, the rest of str is used.
tolower(str)
returns a copy of the string str, with all the upper-case characters in str translated to their corresponding lower-case counterparts. Non-alphabetic characters are left unchanged.
toupper(str)
returns a copy of the string str, with all the lower-case characters in str translated to their corresponding upper-case counterparts. Non-alphabetic characters are left unchanged.

The I/O related functions are:

close(expr)
Close the open file or pipe denoted by expr.
fflush([expr])
Flush any buffered output for the output file or pipe denoted by expr. If expr is omitted, standard output is flushed. If expr is the null string (""), all output buffers are flushed.
system(cmd-line)
Execute the command cmd-line, and return the exit status. If your operating system does not support system, calling it will generate a fatal error. `system("")' can be used to force awk to flush any pending output. This is more portable, but less obvious, than calling fflush.


Go to the first, previous, next, last section, table of contents.