awk(1)

awk(1)

The print statement prints its arguments on the standard output (or on a ®le if >®le or >>®le is present or on a pipe if cmd is present), separated by the current output ®eld separator, and terminated by the output record separator. ®le and cmd can be literal names or parenthesized expressions. Identical string values in different statements denote the same open ®le. The printf statement formats its expression list according to the format (see printf(3)).

Built-In Functions

The built-in function close(expr) closes the ®le or pipe expr opened by a print or printf statement or a call to getline with the same string-valued expr. This function returns zero if successful, oth- erwise, it returns non-zero.

The customary functions exp, log, sqrt, sin, cos, atan2 are built in. Other built-in functions are:

blength [( [ s ] ) ]

Length of its associated argument (in bytes) taken as a string, or of $0 if no argu- ment.

length [( [ s ] ) ]

Length of its associated argument (in characters) taken as a string, or of $0 if no

 

argument.

rand()

Returns a random number between zero and one.

srand( [ expr ] )

Sets the seed value for rand, and returns the previous seed value. If no argument is

 

given, the time of day is used as the seed value; otherwise, expr is used.

int(x)

Truncates to an integer value

substr(s, m [, n] )

Return the at most n-character substring of s that begins at position m, numbering from 1. If n is omitted, the substring is limited by the length of string s.

index( s, t ) Return the position, in characters, numbering from 1, in string s where string t ®rst occurs, or zero if it does not occur at all.

match( s, ere ) Return the position, in characters, numbering from 1, in string s where the extended regular expression ere occurs, or 0 if it does not. The variables RSTART and RLENGTH are set to the position and length of the matched string.

split( s, a[ , fs] )

Splits the string s into array elements a[1], a[2], ..., a[n], and returns n. The separation is done with the regular expression fs, or with the ®eld separator FS if fs is not given.

sub( ere, repl [ , in] )

 

Substitutes repl for the ®rst occurrence of the extended regular expression ere in the

 

string in. If in is not given, $0 is used.

gsub

Same as sub except that all occurrences of the regular expression are replaced;

 

sub and gsub return the number of replacements.

sprintf( fmt, expr, ... )

String resulting from formatting expr ... according to the printf(3S) format fmt

system(cmd) Executes cmd and returns its exit status

toupper(s) Converts the argument string s to uppercase and returns the result.

tolower(s) Converts the argument string s to lowercase and returns the result.

The built-in function getline sets $0 to the next input record from the current input ®le; getline < ®le sets $0 to the next record from ®le. getline x sets variable x instead. Finally, cmd getline pipes the output of cmd into getline; each call of getline returns the next line of output from cmd. In all cases, getline returns 1 for a successful input, 0 for end of ®le, and −1 for an error.

Patterns

Patterns are arbitrary Boolean combinations (with ! &&) of regular expressions and relational expres- sions. awk supports Extended Regular Expressions as described in regexp(5). Isolated regular expressions in a pattern apply to the entire line. Regular expressions can also occur in relational expressions, using the operators Ä and . /re/ is a constant regular expression; any string (constant or variable) can be used as a regular expression, except in the position of an isolated regular expression in a pattern.

HP-UX Release 11i: December 2000

− 2 −

Section 135

a