Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Section B.3. Command Summary for awk

B.3. Command Summary for awk

The following alphabetical list of statements and functions includes all that are available in POSIX awk, nawk, or gawk. See Chapter 11, for extensions available in different implementations.

atan2( )

atan2(y, x)

Returns the arctangent of y/x in radians.


Exit from a while, for, or do loop.

close( )



In most implementations of awk, you can only have a limited number of files and/or pipes open simultaneously. Therefore, awk provides a close( ) function that allows you to close a file or a pipe. It takes as an argument the same expression that opened the pipe or file. This expression must be identical, character by character, to the one that opened the file or pipe—even whitespace is significant.


Begin next iteration of while, for, or do loop.

cos( )


Return cosine of x in radians.


delete array[element]

Delete element of an array.


while (expr)

Looping statement. Execute statements in body then evaluate expr and if true, execute body again.


exit [expr]

Exit from script, reading no new input. The END rule, if it exists, will be executed. An optional expr becomes awk's return value.

exp( )


Return exponential of x (e ^ x).


for (init-expr; test-expr; incr-expr) statement

C-style looping construct. init-expr assigns the initial value of the counter variable. test-expr is a relational expression that is evaluated each time before executing the statement. When test-expr is false, the loop is exited. incr-expr is used to increment the counter variable after each pass.

for (item in array) statement

Special loop designed for reading associative arrays. For each element of the array, the statement is executed; the element can be referenced by array[item].


Read next line of input.

getline [var] [<file]

command | getline [var]

The first form reads input from file and the second form reads the output of command. Both forms read one line at a time, and each time the statement is executed it gets the next line of input. The line of input is assigned to $0 and it is parsed into fields, setting NF, NR, and FNR. If var is specified, the result is assigned to var and the $0 is not changed. Thus, if the result is assigned to a variable, the current line does not change. getline is actually a function and it returns 1 if it reads a record successfully, 0 if end-of-line is encountered, and -1 if for some reason it is otherwise unsuccessful.

gsub( )

gsub(r, s, t)

Globally substitute s for each match of the regular expression r in the string t. Return the number of substitutions. If t is not supplied, defaults to $0.


if (expr) statement1

[ else statement2 ]

Conditional statement. Evaluate expr and, if true, execute statement1; if else clause is supplied, execute statement2 if expr is false.

index( )

index(str, substr)

Return position (starting at 1) of substring in string.

int( )


Return integer value of x by truncating any digits following a decimal point.

length( )


Return length of string, or the length of $0 if no argument.

log( )


Return natural logarithm (base e) of x.

match( )

match(s, r)

Function that matches the pattern, specified by the regular expression r, in the string s and returns either the position in s where the match begins, or 0 if no occurrences are found. Sets the values of RSTART and RLENGTH to the start and length of the match, respectively.


Read next input line and begin executing script at first rule.


print [ output-expr ] [ dest-expr ]

Evaluate the output-expr and direct it to standard output followed by the value of ORS. Each output-expr is separated by the value of OFS. dest-expr is an optional expression that directs the output to a file or pipe. "> file" directs the output to a file, overwriting its previous contents. ">> file" appends the output to a file, preserving its previous contents. In both of these cases, the file will be created if it does not already exist. "| command" directs the output as the input to a system command.


printf (format-expr [, expr-list ]) [ dest-expr ]

An alternative output statement borrowed from the C language. It has the ability to produce formatted output. It can also be used to output data without automatically producing a newline. format-expr is a string of format specifications and constants; see next section for a list of format specifiers. expr-list is a list of arguments corresponding to format specifiers. See the print statement for a description of dest-expr.

rand( )

rand( )

Generate a random number between 0 and 1. This function returns the same series of numbers each time the script is executed, unless the random number generator is seeded using the srand( ) function.


return [expr]

Used at end of user-defined functions to exit function, returning value of expression.

sin( )


Return sine of x in radians.

split( )

split(str, array, sep)

Function that parses string into elements of array using field separator, returning number of elements in array. Value of FS is used if no field separator is specified. Array splitting works the same as field splitting.

sprintf( )

sprintf (format-expr [, expr-list ] )

Function that returns string formatted according to printf format specification. It formats data but does not output it. format-expr is a string of format specifications and constants; see the next section for a list of format specifiers. expr-list is a list of arguments corresponding to format specifiers.

sqrt( )


Return square root of x.

srand( )


Use expr to set a new seed for random number generator. Default is time of day. Return value is the old seed.

sub( )

sub(r, s, t)

Substitute s for first match of the regular expression r in the string t. Return 1 if successful; 0 otherwise. If t is not supplied, defaults to $0.

substr( )

substr(str, beg, len)

Return substring of string str at beginning position beg, and the characters that follow to maximum specified length len. If no length is given, use the rest of the string.

system( )


Function that executes the specified command and returns its status. The status of the executed command typically indicates success or failure. A value of 0 means that the command executed successfully. A non-zero value, whether positive or negative, indicates a failure of some sort. The documentation for the command you're running will give you the details. The output of the command is not available for processing within the awk script. Use "command | getline" to read the output of a command into the script.

tolower( )


Translate all uppercase characters in str to lowercase and return the new string.[3]

[3] Very early versions of nawk, such as that in SunOS 4.1.x, don't support tolower( ) and toupper( ). However, they are now part of the POSIX specification for awk.

toupper( )


Translate all lowercase characters in str to uppercase and return the new string.


while (expr) statement

Looping construct. While expr is true, execute statement.


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint