nawk is a new version of awk.1 that provides additional features including, dynamic regular expressions, additional built-ins and operators, and user defined functions. Other implementations refer to this command by its original name, awk, choosing to replace the original program with the enhanced one. Since there is a slight incompatibility between the two versions (see BUGS below) both versions are available in the SunOS environment, the original, awk, and the enhanced, nawk.
nawk scans each input filename for lines that match any of a set of patterns specified in program. program string must be enclosed in single quotes (') to protect it from the shell. For each pattern in program there may be an associated action performed when a line of a filename matches the pattern. The set of pattern-action statements may appear literally as program or in a file specified with the -f program-file option.
Input files are read in order; if there are no files, the standard input is read. The file name `-' means the standard input. Each input line is matched against the pattern portion of every pattern-action statement; the associated action is performed for each matched pattern.
An input line is normally made up of fields separated by white space. This default can be changed by using the FS built-in variable or the -F c option.) The fields are denoted $1, $2, ...; $0 refers to the entire line.
nawk programs contain pattern-action statements of the form:
Either pattern or action may be omitted. If there is no action with a pattern, the matching line is printed. If there is no pattern with an action, the action is performed on every input line.
Patterns are arbitrary Boolean combinations (!, ||, &&, and parentheses) of relational expressions and regular expressions. A relational expression is one of the following:
expression relop expression expression matchop regular expression expression in array-name (expression, expression, ...) in array-name
where relop is any of the six relational operators in C, and matchop is either ~ (contains) or !~ (does not contain). An expression is an arithmetic expression, a relational expression, the special expression
or a Boolean combination of these.
The special patterns BEGIN and END may be used to capture control before the first input line has been read and after the last input line has been read respectively. They are the only patterns that require an action statement. These keywords do not combine with any other patterns.
Regular expressions are as in egrep (see grep.1v In patterns they must be surrounded by slashes. Isolated regular expressions in a pattern apply to the entire line. Regular expressions may also occur in relational expressions. A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines between an occurrence of the first pattern and the next occurrence of the second pattern.
An action is a sequence of statements. A statement may be one of the following:
if ( expression ) statement [ else statement ] while ( expression ) statement do statement while ( expression ) for ( expression ; expression ; expression ) statement for ( var in array ) statement delete array[subscript] break continue { [ statement ] ... } expression # commonly variable = expression print [ expression-list ] [ >expression ] printf format [ , expression-list ] [ >expression ] next # skip remaining patterns on this input line exit [expr] # skip the rest of the input; exit status is expr return [expr]
Statements are terminated by semicolons, right braces, or NEWLINE characters. An empty expression-list stands for the whole input line. Expressions take on string or numeric values as appropriate, and are built using the operators +, -, *, /, %, and concatenation (indicated by a blank). The C operators ++, --, +=, -=, *=, /=, and %= are also available in expressions. Variables may be scalars, array elements (denoted x[i]), or fields. Variables are initialized to the null string or zero. Array subscripts may be any string, not necessarily numeric; this allows for a form of associative memory. String constants are quoted (").
The print statement prints its arguments on the standard output, or on a file if >expression is present, or on a pipe if `| cmd' is present. The arguments are separated by the current output field separator and terminated by the output record separator. The printf statement formats its expression list according to the format (see printf.3v
A regular expression may be used to separate fields by using the -F c option or by assigning the expression to the built-in variable FS. The default is to ignore leading blanks and to separate fields by blanks and/or tab characters. However, if FS is assigned a value, leading blanks are no longer ignored.
Built-in variables include:
nawk has a variety of built-in functions: arithmetic, string, input/output, and general.
The arithmetic functions are: atan2, cos, exp, int, log, rand, sin, sqrt, and srand. int truncates its argument to an integer. rand returns a random number between 0 and 1. srand ( expr ) sets the seed value for rand to expr or uses the time of day if expr is omitted.
The string functions are:
The input/output and general functions are:
All forms of
getline
return 1 for successful input, 0 for end of file, and -1 for an error.
nawk also provides user-defined functions. Such functions may be defined (in the pattern position of a pattern-action statement) as
function name(args,...) { stmts } func name(args,...) { stmts }
Function arguments are passed by value if scalar and by reference if array name. Argument names are local to the function; all other variable names are global. Function calls may be nested and functions may be recursive. The return statement may be used to return a value.
Print lines longer than 72 characters:
Print first two fields in opposite order:
Same, with input fields separated by comma and/or blanks and tabs:
Add up first column, print sum and average:
Print fields in reverse order:
Print all lines between start/stop pairs:
Print all lines whose first field is different from previous one:
Simulate echo.1v
BEGIN { for (i = 1; i < ARGC; i++) printf "%s", ARGV[i] printf "\n" exit }
Print file, filling in page numbers starting at 5:
example% nawk -f program n=5 input
[a manual with the abbreviation TEXT]
A. V. Aho, B. W. Kerninghan, P. J. Weinberger,
The AWK Programming Language
Addison-Wesley, 1988.
Input white space is not preserved on output if fields are involved.
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 the null string ("") to it.
Pattern-action statements must be separated by either a semi-colon or a NEWLINE. This is an incompatibility with the old version of awk.
Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97