.\"
.\" Sccsid @(#)nawk.1	1.21 (gritter) 2/6/05
.\" Derived from awk.1, Bell Labs:
.\"
.\" Copyright (C) Lucent Technologies 1997
.\" All Rights Reserved
.\" 
.\" Permission to use, copy, modify, and distribute this software and
.\" its documentation for any purpose and without fee is hereby
.\" granted, provided that the above copyright notice appear in all
.\" copies and that both that the copyright notice and this
.\" permission notice and warranty disclaimer appear in supporting
.\" documentation, and that the name Lucent Technologies or any of
.\" its entities not be used in advertising or publicity pertaining
.\" to distribution of the software without specific, written prior
.\" permission.
.\" 
.\" LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
.\" INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
.\" IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
.\" SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
.\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
.\" ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
.\" THIS SOFTWARE.
.TH NAWK 1 "2/6/05" "Heirloom Toolchest" "User Commands"
.SH NAME
nawk \- pattern scanning and processing language
.SH SYNOPSIS
.HP
.ad l
\fBnawk\fR
[\fB\-f \fIprogfile\fR | \fI'prog'\fR]
[\fB\-F\fIfieldsep\fR]
[\fB\-v \fIvar=value\fR]
[\fIfile . . .\fR]
.br
.ad b
.SH DESCRIPTION
.I Nawk
scans each input
.I file
for lines that match any of a set of patterns specified literally in
.IR prog
or in one or more files
specified as
.B \-f
.IR progfile .
With each pattern
there can be an associated action that will be performed
when a line of a
.I file
matches the pattern.
Each line is matched against the
pattern portion of every pattern-action statement;
the associated action is performed for each matched pattern.
The file name 
.B \-
means the standard input.
Any
.IR file
of the form
.I var=value
is treated as an assignment, not a filename,
and is executed at the time it would have been opened if it were a filename
.RB ( /usr/5bin/s42/awk ,
.BR /usr/5bin/posix/awk ,
and
.B /usr/5bin/posix2001/awk
only).
The option
.B \-v
followed by
.I var=value
is an assignment to be done before
.I prog
is executed;
any number of
.B \-v
options may be present.
The
.B \-F
.IR fs
option defines the input field separator to be the regular expression
.IR fs.
.PP
An input line is normally made up of fields separated by white space,
or by regular expression
.BR FS .
The fields are denoted
.BR $1 ,
.BR $2 ,
\&..., while
.B $0
refers to the entire line.
.PP
A pattern-action statement has the form
.IP
.IB pattern " { " action " }"
.PP
A missing 
.BI { " action " }
means print the line;
a missing pattern always matches.
Pattern-action statements are separated by newlines or semicolons.
.PP
An action is a sequence of statements.
A statement can be one of the following:
.PP
.\".ta \w'\f(CWdelete array[subscript]'u
.RS
.nf
\fBif (\fI expression \fB)\fI statement \fR[ \fBelse\fI statement \fR]
\fBwhile (\fI expression \fB)\fI statement\fR
\fBfor (\fI expression \fB;\fI expression \fB;\fI expression \fB)\fI statement\fR
\fBfor (\fI var \fBin\fI array \fB)\fI statement\fR
\fBdo\fI statement \fBwhile (\fI expression \fB)\fR
\fBbreak\fR
\fBcontinue\fR
\fB{\fR [\fIstatement \fR...] \fB}\fR
\fIexpression\fR	# commonly \fIvar \fB=\fI expression\fR
\fBprint\fR [\fIexpression-list\fR] [\fB>\fI expression\fR]
\fBprintf\fI format \fR[\fB,\fI expression-list\fR] [\fB>\fI expression\fR]
\fBnext	\fR# skip remaining patterns on this input line
\fBdelete\fI array\fB[\fIsubscript\fB]\fR	# delete an array element
\fBexit\fR [\fIexpr\fR]	# exit immediately; status is \fIexpr\fR
\fBreturn\fR [\fIexpr\fR]
.fi
.RE
.br
.DT
.PP
Statements are terminated by
semicolons, newlines or right braces.
An empty
.I expression-list
stands for
.BR $0 .
String constants are quoted \&\f(CW"\ "\fR,
with the usual C escapes recognized within.
Expressions take on string or numeric values as appropriate,
and are built using the operators
.B + \- * / % ^
(exponentiation), and concatenation (indicated by white space).
The operators
.B ! ++ \-\- += \-= *=
.B /= %= ^= > >= <
.B <= == != ?:
are also available in expressions.
Variables may be scalars, array elements
(denoted \fIx\fB[\fIi\fB]\fR)
or fields.
Variables are initialized to the null string.
Array subscripts may be any string,
not necessarily numeric;
this allows for a form of associative memory.
Multiple subscripts such as
\fB[\fIi\fB,\fIj\fB,\fIk\fB]\fR
are permitted; the constituents are concatenated,
separated by the value of
.BR SUBSEP .
.PP
The
.B print
statement prints its arguments on the standard output
(or on a file if
.BI > file
or
.BI >> file
is present or on a pipe if
.BI | cmd
is present), separated by the current output field separator,
and terminated by the output record separator.
.I file
and
.I cmd
may be literal names or parenthesized expressions;
identical string values in different statements denote
the same open file.
The
.B printf
statement formats its expression list according to the format
(see
.IR printf (3)) .
The built-in function
.BI close( expr )
closes the file or pipe
.IR expr .
.PP
The mathematical functions
.BR exp ,
.BR log ,
.BR sqrt ,
.BR sin ,
.BR cos ,
and
.BR atan2 
are built in.
Other built-in functions:
.\".TF length
.TP
.B gsub
same as
.B sub
except that all occurrences of the regular expression
are replaced;
.B sub
and
.B gsub
return the number of replacements.
.TP
.BI index( s , " t" )
the position in
.I s
where the string
.I t
occurs, or 0 if it does not.
.TP
.B int
truncates to an integer value
.TP
.B length
the length of its argument
taken as a string,
or of
.B $0
if no argument.
.TP
.BI match( s , " r" )
the position in
.I s
where the regular expression
.I r
occurs, or 0 if it does not.
The variables
.B RSTART
and
.B RLENGTH
are set to the position and length of the matched string.
.TP
.B rand
random number on (0,1)
.TP
\fBsplit(\fIs\fB, \fIa\fB, \fIfs\fB)\fR
splits the string
.I s
into array elements
.IB a [1] ,
.IB a [2] ,
\&...,
.IB a [ n ] ,
and returns
.IR n .
The separation is done with the regular expression
.I fs
or with the field separator
.B FS
if
.I fs
is not given.
.TP
\fBsprintf(\fIfmt\fB, \fIexpr\fB, \fI...\fB)\fR
the string resulting from formatting
.I expr ...
according to the
.IR printf (3)
format
.I fmt
.TP
.B srand
sets seed for
.B rand
and returns the previous seed.
.TP
\fBsub(\fIr\fB, \fIt\fB, \fIs\fB)\fR
substitutes
.I t
for the first occurrence of the regular expression
.I r
in the string
.IR s .
If
.I s
is not given,
.B $0
is used.
.TP
\fBsubstr(\fIs\fB, \fIm\fB, \fIn\fB)\fR
the
.IR n -character
substring of
.I s
that begins at position
.IR m 
counted from 1.
.TP
.BI system( cmd )
executes
.I cmd
and returns its exit status
.TP
.BI tolower( str )
returns a copy of
.I str
with all upper-case characters translated to their
corresponding lower-case equivalents.
.TP
.BI toupper( str )
returns a copy of
.I str
with all lower-case characters translated to their
corresponding upper-case equivalents.
.PD
.PP
The ``function''
.B getline
sets
.B $0
to the next input record from the current input file;
.B getline
.BI < file
sets
.B $0
to the next record from
.IR file .
.B getline
.I x
sets variable
.I x
instead.
Finally,
.IB cmd " |getline"
pipes the output of
.I cmd
into
.BR getline ;
each call of
.B getline
returns the next line of output from
.IR cmd .
In all cases,
.B getline
returns 1 for a successful input,
0 for end of file, and \-1 for an error.
.PP
Additional functions may be defined
(at the position of a pattern-action statement) thus:
.IP
\fBfunction \fIfoo\fB(\fIa\fB, \fIb\fB, \fIc\fB)
{ \fI...\fB; return \fIx\fB }\fR
.PP
or:
.IP
\fBfunc \fIfoo\fB(\fIa\fB, \fIb\fB, \fIc\fB)
{ \fI...\fB; return \fIx\fB }\fR
.PP
Parameters are passed by value if scalar and by reference if array name;
functions may be called recursively.
Parameters are local to the function; all other variables are global.
Thus local variables may be created by providing excess parameters in
the function definition.
.PP
Patterns are arbitrary Boolean combinations
(with
.BR "! || &&" )
of regular expressions and
relational expressions.
Regular expressions are
full regular expressions with
.B /usr/5bin/nawk
and
extended regular expressions with
.BR /usr/5bin/s42/awk ,
.BR /usr/5bin/posix/awk ,
and
.BR /usr/5bin/posix2001/awk ;
both are as described in
.IR egrep (1).
Isolated regular expressions
in a pattern apply to the entire line.
Regular expressions may also occur in
relational expressions, using the operators
.BR ~
and
.BR !~ .
.BI / re /
is a constant regular expression;
any string (constant or variable) may be used
as a regular expression, except in the position of an isolated regular expression
in a pattern.
For
.BR /usr/5bin/posix2001/awk ,
regular expressions may be part of arithmetic expressions.
.PP
A pattern may consist of two patterns separated by a comma;
in this case, the action is performed for all lines
from an occurrence of the first pattern
though an occurrence of the second.
.PP
A relational expression is one of the following:
.IP
.I expression matchop regular-expression
.br
.I expression relop expression
.br
.IB expression " in " array-name
.br
.BI ( expr , expr,... ") in " array-name
.PP
where a relop is any of the six relational operators in C,
and a matchop is either
.B ~
(matches)
or
.B !~
(does not match).
A conditional is an arithmetic expression,
a relational expression,
or a Boolean combination
of these.
.PP
The special patterns
.B BEGIN
and
.B END
may be used to capture control before the first input line is read
and after the last.
.B BEGIN
and
.B END
do not combine with other patterns.
.PP
Variable names with special meanings:
.\".TF FILENAME
.TP 10
.B ARGC
argument count, assignable
.TP 10
.B ARGV
argument array, assignable;
non-null members are taken as filenames
.TP 10
.B CONVFMT
.RB ( /usr/5bin/s42/awk ,
.BR /usr/5bin/posix2001/awk ,
and
.B /usr/5bin/posix/awk
only)
conversion format used when converting numbers
(default
.BR "%.6g" )
.TP 10
.B ENVIRON
array of environment variables; subscripts are names.
.TP 10
.B FILENAME
the name of the current input file
.TP 10
.B FNR
ordinal number of the current record in the current file
.TP 10
.B FS
regular expression used to separate fields; also settable
by option
.BI \-F fs.
.TP 10
.BR NF
number of fields in the current record
.TP 10
.B NR
ordinal number of the current record
.TP 10
.B OFMT
output format for numbers (default
.BR "%.6g" )
.TP 10
.B OFS
output field separator (default blank)
.TP 10
.B ORS
output record separator (default newline)
.TP 10
.B RS
input record separator (default newline)
.TP 10
.B SUBSEP
separates multiple subscripts (default 034)
.PD
.SH EXAMPLES
.TP
.nf
length($0) > 72
.br
.fi
Print lines longer than 72 characters.
.TP
.nf
{ print $2, $1 }
.br
.fi
Print first two fields in opposite order.
.PP
.nf
BEGIN { FS = ",[ \et]*|[ \et]+" }
      { print $2, $1 }
.br
.fi
.ns
.IP
Same, with input fields separated by comma and/or blanks and tabs.
.PP
.nf
	{ s += $1 }
END	{ print "sum is", s, " average is", s/NR }
.fi
.br
.ns
.IP
Add up first column, print sum and average.
.TP
.nf
/start/, /stop/
.br
.fi
Print all lines between start/stop pairs.
.PP
.nf
BEGIN	{	# Simulate echo(1)
	for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
	printf "\en"
	exit }
.fi
.br
.SH "ENVIRONMENT VARIABLES"
.TP 
.BR LANG ", " LC_ALL
See
.IR locale (7).
.TP
.B LC_COLLATE
Affects the collation order for range expressions,
equivalence classes, and collation symbols
in regular expressions
as well as string comparison.
.TP
.B LC_CTYPE
Determines the mapping of bytes to characters,
the availability and composition of character classes
in regular expressions,
and the case mapping for the toupper() and tolower() functions.
.TP 
.B LC_NUMERIC
Determine the radix character used when interpreting numeric input,
performing conversions between numeric and string values
and formatting numeric output.
Regardless of locale, the period character
(the decimal-point character of the C locale)
is the decimal-point character recognized in processing awk programs.
.SH SEE ALSO
egrep(1),
lex(1),
oawk(1),
sed(1),
printf(3),
locale(7)
.br
A. V. Aho, B. W. Kernighan, P. J. Weinberger,
.I
The AWK Programming Language,
Addison-Wesley, 1988.  ISBN 0-201-07981-X
.SH NOTES
There are no explicit conversions between numbers and strings.
To force an expression to be treated as a number add 0 to it;
to force it to be treated as a string concatenate
\&\fB""\fR to it.
.\".sp
.\"The scope rules for variables in functions are a botch;
.\"the syntax is worse.
.PP
The LC_COLLATE variable has currently no effect in regular expressions.
Ranges in bracket expressions are ordered
as byte values in single-byte locales
and as wide character values in multibyte locales;
equivalence classes match the given character only,
and multi-character collating elements are not available.
