An Awk Primer/Standard Functions

< An Awk Primer

Below is the list of Awk functions. Arguments which can be omitted are in square brackets.

Numerical functions

Numerical functions work with numbers. All of them return a number and have only numerical parameters, or no parameters at all.

atan2

atan2(y,x) returns the angle \alpha, in radians, such that:

The formulas are

\operatorname{atan2}(y, x) = \begin{cases}
\arctan\left(\frac y x\right) & \qquad x > 0 \\
\arctan\left(\frac y x\right) + \pi& \qquad y \ge 0 , x < 0 \\
\arctan\left(\frac y x\right) - \pi& \qquad y < 0 , x < 0 \\
\frac{\pi}{2} \sgn{y}& \qquad x = 0
\end{cases}

String functions

String functions work with strings. All of them have at least one string parameter, which sometimes can be omitted. For most of them, all parameters are strings and/or regular expressions.

Note that in Awk strings, characters are numbered from 1. For example, in the string "cat", the character number 1 is "c", the character number 2 is "a", the character number 3 is "t".

Below, s and t are strings, regexp is a regular expression.

GNU Awk extensions

String function

gensub(regexp, s, h [, t]) replaces the h-th match of regexp by s in the string t ($0 by default). For example, gensub(/o/, "O", 3, t) replaces the third "o" by "O" in t.

Examples:

Array functions

Below, A and B are arrays.

Other standard functions

GNU Awk also has:

See the man page (man gawk) for more information.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.