a

awk(1)

awk(1)

NAME

awk - pattern-directed scanning and processing language

SYNOPSIS

awk [-Ffs ] [-vvar=value ] [ program -fprog®le ... ] [ ®le ... ]

DESCRIPTION

awk scans each input ®le for lines that match any of a set of patterns speci®ed literally in program or in one or more ®les speci®ed as -fprog®le. With each pattern there can be an associated action that is to be performed when a line in a ®le matches the pattern. Each line is matched against the pattern portion of every pattern-action statement, and the associated action is performed for each matched pattern. The ®le name - means the standard input. Any ®le of the form var=value is treated as an assignment, not a ®lename. An assignment is evaluated at the time it would have been opened if it were a ®lename, unless the -voption is used.

An input line is made up of ®elds separated by white space, or by regular expression FS. The ®elds are denoted $1, $2, ...; $0 refers to the entire line.

Options

awk recognizes the following options and arguments:

-F fs

Specify regular expression used to separate ®elds. The default is to recognize space

 

and tab characters, and to discard leading spaces and tabs. If the -Foption is used,

 

leading input ®eld separators are no longer discarded.

-fprogfile

Specify an awk program ®le. Up to 100 program ®les can be speci®ed. The pattern-

 

action statements in these ®les are executed in the same order as the ®les were

 

speci®ed.

-vvar=value Cause var=value assignment to occur before the BEGIN action (if it exists) is exe- cuted.

Statements

A pattern-action statement has the form:

pattern { action }

A missing { action } means print the line; a missing pattern always matches. Pattern-action statements are separated by new-lines or semicolons.

An action is a sequence of statements. A statement can be one of the following:

if( expression ) statement [ else statement ] while( expression) statement

for( expression ; expression ; expression) statement

for( var in array ) statement

 

do statement

while( expression )

 

break

 

 

continue

 

 

{ [ statement

... ] }

 

expression

 

# commonly var = expression

print [ expression-list ] [ > expression ]

printf format [, expression-list ]

[ > expression ]

return [ expression ]

 

next

[ expression ]

# skip remaining patterns on this input line.

delete array

# delete an array element.

exit [ expression ]

# exit immediately; status is expression.

Statements are terminated by semicolons, newlines or right braces. An empty expression-liststands for $0. String constants are quoted (" "), with the usual C escapes recognized within. Expressions take on string or numeric values as appropriate, and are built using the operators +, -, *, /, %, Ã (exponentiation), and concatenation (indicated by a blank). The operators ++, - -, +=, -=, *=, /=, %=, Ã=, **=, >, >=, <, <=, ==, !=, and ?: are also available in expressions. Variables can be scalars, array elements (denoted x[i]) or ®elds. Variables are initialized to the null string. Array subscripts can be any string, not necessarily numeric (this allows for a form of associative memory). Multiple subscripts such as [ i,j,k ] are permitted. The constituents are concatenated, separated by the value of SUBSEP.

Section 134

− 1 −

HP-UX Release 11i: December 2000