2 .\" Copyright 1989 AT&T
3 .\" Copyright 1992, X/Open Company Limited All Rights Reserved
4 .\" Portions Copyright (c) 2005, 2006 Sun Microsystems, Inc. All Rights Reserved
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
8 .\" This notice shall appear on any product containing this material.
9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH NAWK 1 "May 24, 2006"
14 nawk \- pattern scanning and processing language
18 \fB/usr/bin/nawk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR] \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
24 \fB/usr/xpg4/bin/awk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR]... \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
31 The \fB/usr/bin/nawk\fR and \fB/usr/xpg4/bin/awk\fR utilities execute
32 \fIprogram\fRs written in the \fBnawk\fR programming language, which is
33 specialized for textual data manipulation. A \fBnawk\fR \fIprogram\fR is a
34 sequence of patterns and corresponding actions. The string specifying
35 \fIprogram\fR must be enclosed in single quotes (') to protect it from
36 interpretation by the shell. The sequence of pattern - action statements can be
37 specified in the command line as \fIprogram\fR or in one, or more, file(s)
38 specified by the \fB-f\fR\fIprogfile\fR option. When input is read that matches
39 a pattern, the action associated with the pattern is performed.
42 Input is interpreted as a sequence of records. By default, a record is a line,
43 but this can be changed by using the \fBRS\fR built-in variable. Each record of
44 input is matched to each pattern in the \fIprogram\fR. For each pattern
45 matched, the associated action is executed.
48 The \fBnawk\fR utility interprets each input record as a sequence of fields
49 where, by default, a field is a string of non-blank characters. This default
50 white-space field delimiter (blanks and/or tabs) can be changed by using the
51 \fBFS\fR built-in variable or the \fB-F\fR\fIERE\fR option. The \fBnawk\fR
52 utility denotes the first field in a record \fB$1\fR, the second \fB$2\fR, and
53 so forth. The symbol \fB$0\fR refers to the entire record; setting any other
54 field causes the reevaluation of \fB$0\fR. Assigning to \fB$0\fR resets the
55 values of all fields and the \fBNF\fR built-in variable.
59 The following options are supported:
63 \fB\fB-F\fR \fIERE\fR\fR
66 Define the input field separator to be the extended regular expression
67 \fIERE\fR, before any input is read (can be a character).
73 \fB\fB-f\fR \fIprogfile\fR\fR
76 Specifies the pathname of the file \fIprogfile\fR containing a \fBnawk\fR
77 program. If multiple instances of this option are specified, the concatenation
78 of the files specified as \fIprogfile\fR in the order specified is the
79 \fBnawk\fR program. The \fBnawk\fR program can alternatively be specified in
80 the command line as a single argument.
86 \fB\fB-v\fR \fIassignment\fR\fR
89 The \fIassignment\fR argument must be in the same form as an \fIassignment\fR
90 operand. The assignment is of the form \fIvar=value\fR, where \fIvar\fR is the
91 name of one of the variables described below. The specified assignment occurs
92 before executing the \fBnawk\fR program, including the actions associated with
93 \fBBEGIN\fR patterns (if any). Multiple occurrences of this option can be
100 The following operands are supported:
107 If no \fB-f\fR option is specified, the first operand to \fBnawk\fR is the text
108 of the \fBnawk\fR program. The application supplies the \fIprogram\fR operand
109 as a single argument to \fBnawk.\fR If the text does not end in a newline
110 character, \fBnawk\fR interprets the text as if it did.
119 Either of the following two types of \fIargument\fR can be intermixed:
126 A pathname of a file that contains the input to be read, which is matched
127 against the set of patterns in the program. If no \fIfile\fR operands are
128 specified, or if a \fIfile\fR operand is \fB\(mi\fR, the standard input is
135 \fB\fIassignment\fR\fR
138 An operand that begins with an underscore or alphabetic character from the
139 portable character set, followed by a sequence of underscores, digits and
140 alphabetics from the portable character set, followed by the \fB=\fR character
141 specifies a variable assignment rather than a pathname. The characters before
142 the \fB=\fR represent the name of a \fBnawk\fR variable. If that name is a
143 \fBnawk\fR reserved word, the behavior is undefined. The characters following
144 the equal sign is interpreted as if they appeared in the \fBnawk\fR program
145 preceded and followed by a double-quote (\fB"\fR) character, as a \fBSTRING\fR
146 token , except that if the last character is an unescaped backslash, it is
147 interpreted as a literal backslash rather than as the first character of the
148 sequence \fB\e\fR\&.. The variable is assigned the value of that \fBSTRING\fR
149 token. If the value is considered a \fInumeric\fRstring\fI,\fR the variable is
150 assigned its numeric value. Each such variable assignment is performed just
151 before the processing of the following \fIfile\fR, if any. Thus, an assignment
152 before the first \fBfile\fR argument is executed after the \fBBEGIN\fR actions
153 (if any), while an assignment after the last \fIfile\fR argument is executed
154 before the \fBEND\fR actions (if any). If there are no \fIfile\fR arguments,
155 assignments are executed before processing the standard input.
163 Input files to the \fBnawk\fR program from any of the following sources:
168 any \fIfile\fR operands or their equivalents, achieved by modifying the
169 \fBnawk\fR variables \fBARGV\fR and \fBARGC\fR
175 standard input in the absence of any \fIfile\fR operands
181 arguments to the \fBgetline\fR function
185 must be text files. Whether the variable \fBRS\fR is set to a value other than
186 a newline character or not, for these files, implementations support records
187 terminated with the specified separator up to \fB{LINE_MAX}\fR bytes and can
188 support longer records.
191 If \fB-\fR\fBf\fR \fIprogfile\fR is specified, the files named by each of the
192 \fIprogfile\fR option-arguments must be text files containing an \fBnawk\fR
196 The standard input are used only if no \fIfile\fR operands are specified, or if
197 a \fIfile\fR operand is \fB\(mi\fR\&.
198 .SH EXTENDED DESCRIPTION
201 A \fBnawk\fR program is composed of pairs of the form:
205 pattern { \fIaction\fR }
211 Either the pattern or the action (including the enclosing brace characters) can
212 be omitted. Pattern-action statements are separated by a semicolon or by a
216 A missing pattern matches any record of input, and a missing action is
217 equivalent to an action that writes the matched record of input to standard
221 Execution of the \fBnawk\fR program starts by first executing the actions
222 associated with all \fBBEGIN\fR patterns in the order they occur in the
223 program. Then each \fIfile\fR operand (or standard input if no files were
224 specified) is processed by reading data from the file until a record separator
225 is seen (a newline character by default), splitting the current record into
226 fields using the current value of \fBFS\fR, evaluating each pattern in the
227 program in the order of occurrence, and executing the action associated with
228 each pattern that matches the current record. The action for a matching pattern
229 is executed before evaluating subsequent patterns. Last, the actions associated
230 with all \fBEND\fR patterns is executed in the order they occur in the program.
231 .SS "Expressions in nawk"
234 Expressions describe computations used in \fIpatterns\fR and \fIactions\fR. In
235 the following table, valid expression operations are given in groups from
236 highest precedence first to lowest precedence last, with equal-precedence
237 operators grouped between horizontal lines. In expression evaluation, where the
238 grammar is formally ambiguous, higher precedence operators are evaluated before
239 lower precedence operators. In this table \fIexpr,\fR \fIexpr1,\fR
240 \fIexpr2,\fR and \fIexpr3\fR represent any expression, while \fIlvalue\fR
241 represents any entity that can be assigned to (that is, on the left side of an
242 assignment operator).
249 \fBSyntax\fR \fBName\fR \fBType of Result\fR \fBAssociativity\fR
251 ( \fIexpr\fR ) Grouping type of \fIexpr\fR n/a
253 $\fIexpr\fR Field reference string n/a
255 ++ \fIlvalue\fR Pre-increment numeric n/a
256 \(mi\(mi\fIlvalue\fR Pre-decrement numeric n/a
257 \fIlvalue\fR ++ Post-increment numeric n/a
258 \fIlvalue\fR \(mi\(mi Post-decrement numeric n/a
260 \fIexpr\fR ^ \fIexpr\fR Exponentiation numeric right
262 ! \fIexpr\fR Logical not numeric n/a
263 + \fIexpr\fR Unary plus numeric n/a
264 \(mi \fIexpr\fR Unary minus numeric n/a
266 \fIexpr\fR * \fIexpr\fR Multiplication numeric left
267 \fIexpr\fR / \fIexpr\fR Division numeric left
268 \fIexpr\fR % \fIexpr\fR Modulus numeric left
270 \fIexpr\fR + \fIexpr\fR Addition numeric left
271 \fIexpr\fR \(mi \fIexpr\fR Subtraction numeric left
273 \fIexpr\fR \fIexpr\fR String concatenation string left
275 \fIexpr\fR < \fIexpr\fR Less than numeric none
276 \fIexpr\fR <= \fIexpr\fR Less than or equal to numeric none
277 \fIexpr\fR != \fIexpr\fR Not equal to numeric none
278 \fIexpr\fR == \fIexpr\fR Equal to numeric none
279 \fIexpr\fR > \fIexpr\fR Greater than numeric none
280 \fIexpr\fR >= \fIexpr\fR Greater than or equal to numeric none
282 \fIexpr\fR ~ \fIexpr\fR ERE match numeric none
283 \fIexpr\fR !~ \fIexpr\fR ERE non-match numeric none
285 \fIexpr\fR in array Array membership numeric left
286 ( \fIindex\fR ) in Multi-dimension array numeric left
287 \fIarray\fR membership
289 \fBexpr\fR && \fIexpr\fR Logical AND numeric left
291 \fBexpr\fR |\|| \fIexpr\fR Logical OR numeric left
293 \fIexpr1\fR ? \fIexpr2\fR Conditional expression type of selected right
294 : \fIexpr3\fR \fIexpr2\fR or \fIexpr3\fR
296 \fIlvalue\fR ^= \fIexpr\fR Exponentiation numeric right
298 \fIlvalue\fR %= \fIexpr\fR Modulus assignment numeric right
299 \fIlvalue\fR *= \fIexpr\fR Multiplication numeric right
301 \fIlvalue\fR /= \fIexpr\fR Division assignment numeric right
302 \fIlvalue\fR += \fIexpr\fR Addition assignment numeric right
303 \fIlvalue\fR \(mi= \fIexpr\fR Subtraction assignment numeric right
304 \fIlvalue\fR = \fIexpr\fR Assignment type of \fIexpr\fR right
309 Each expression has either a string value, a numeric value or both. Except as
310 stated for specific contexts, the value of an expression is implicitly
311 converted to the type needed for the context in which it is used. A string
312 value is converted to a numeric value by the equivalent of the following calls:
316 setlocale(LC_NUMERIC, "");
317 \fInumeric_value\fR = atof(\fIstring_value\fR);
323 A numeric value that is exactly equal to the value of an integer is converted
324 to a string by the equivalent of a call to the \fBsprintf\fR function with the
325 string \fB%d\fR as the \fBfmt\fR argument and the numeric value being converted
326 as the first and only \fIexpr\fR argument. Any other numeric value is
327 converted to a string by the equivalent of a call to the \fBsprintf\fR function
328 with the value of the variable \fBCONVFMT\fR as the \fBfmt\fR argument and the
329 numeric value being converted as the first and only \fIexpr\fR argument.
332 A string value is considered to be a \fInumeric string\fR in the following
337 Any leading and trailing blank characters is ignored.
342 If the first unignored character is a \fB+\fR or \fB\(mi\fR, it is ignored.
347 If the remaining unignored characters would be lexically recognized as a
348 \fBNUMBER\fR token, the string is considered a \fInumeric string\fR.
352 If a \fB\(mi\fR character is ignored in the above steps, the numeric value of
353 the \fInumeric string\fR is the negation of the numeric value of the recognized
354 \fBNUMBER\fR token. Otherwise the numeric value of the \fInumeric string\fR is
355 the numeric value of the recognized \fBNUMBER\fR token. Whether or not a string
356 is a \fInumeric string\fR is relevant only in contexts where that term is used
360 When an expression is used in a Boolean context, if it has a numeric value, a
361 value of zero is treated as false and any other value is treated as true.
362 Otherwise, a string value of the null string is treated as false and any other
363 value is treated as true. A Boolean context is one of the following:
368 the first subexpression of a conditional expression.
374 an expression operated on by logical NOT, logical \fBAND,\fR or logical OR.
380 the second expression of a \fBfor\fR statement.
386 the expression of an \fBif\fR statement.
392 the expression of the \fBwhile\fR clause in either a \fBwhile\fR or \fBdo\fR
393 \fB\&.\|.\|.\fR \fBwhile\fR statement.
399 an expression used as a pattern (as in Overall Program Structure).
403 The \fBnawk\fR language supplies arrays that are used for storing numbers or
404 strings. Arrays need not be declared. They are initially empty, and their sizes
405 changes dynamically. The subscripts, or element identifiers, are strings,
406 providing a type of associative array capability. An array name followed by a
407 subscript within square brackets can be used as an \fIlvalue\fR and as an
408 expression, as described in the grammar. Unsubscripted array names are used in
409 only the following contexts:
414 a parameter in a function definition or function call.
420 the \fBNAME\fR token following any use of the keyword \fBin\fR.
424 A valid array \fIindex\fR consists of one or more comma-separated expressions,
425 similar to the way in which multi-dimensional arrays are indexed in some
426 programming languages. Because \fBnawk\fR arrays are really one-dimensional,
427 such a comma-separated list is converted to a single string by concatenating
428 the string values of the separate expressions, each separated from the other by
429 the value of the \fBSUBSEP\fR variable.
432 Thus, the following two index operations are equivalent:
436 var[expr1, expr2, ... exprn]
437 var[expr1 SUBSEP expr2 SUBSEP ... SUBSEP exprn]
443 A multi-dimensioned \fIindex\fR used with the \fBin\fR operator must be put in
444 parentheses. The \fBin\fR operator, which tests for the existence of a
445 particular array element, does not create the element if it does not exist.
446 Any other reference to a non-existent array element automatically creates it.
447 .SS "Variables and Special Variables"
450 Variables can be used in an \fBnawk\fR program by referencing them. With the
451 exception of function parameters, they are not explicitly declared.
452 Uninitialized scalar variables and array elements have both a numeric value of
453 zero and a string value of the empty string.
456 Field variables are designated by a \fB$\fR followed by a number or numerical
457 expression. The effect of the field number \fIexpression\fR evaluating to
458 anything other than a non-negative integer is unspecified. Uninitialized
459 variables or string values need not be converted to numeric values in this
460 context. New field variables are created by assigning a value to them.
461 References to non-existent fields (that is, fields after \fB$NF\fR) produce the
462 null string. However, assigning to a non-existent field (for example,
463 \fB$(NF+2) = 5\fR) increases the value of \fBNF\fR, create any intervening
464 fields with the null string as their values and cause the value of \fB$0\fR to
465 be recomputed, with the fields being separated by the value of \fBOFS\fR. Each
466 field variable has a string value when created. If the string, with any
467 occurrence of the decimal-point character from the current locale changed to a
468 period character, is considered a \fInumeric string\fR (see \fBExpressions in
469 nawk\fR above), the field variable also has the numeric value of the \fInumeric
471 .SS "/usr/bin/nawk, /usr/xpg4/bin/awk"
474 \fBnawk\fR sets the following special variables that are supported by both
475 \fB/usr/bin/nawk\fR and \fB/usr/xpg4/bin/awk\fR:
482 The number of elements in the \fBARGV\fR array.
491 An array of command line arguments, excluding options and the \fIprogram\fR
492 argument, numbered from zero to \fBARGC\fR\(mi1.
494 The arguments in \fBARGV\fR can be modified or added to; \fBARGC\fR can be
495 altered. As each input file ends, \fBnawk\fR treats the next non-null element
496 of \fBARGV\fR, up to the current value of \fBARGC\fR\(mi1, inclusive, as the
497 name of the next input file. Setting an element of \fBARGV\fR to null means
498 that it is not treated as an input file. The name \fB\(mi\fR indicates the
499 standard input. If an argument matches the format of an \fIassignment\fR
500 operand, this argument is treated as an assignment rather than a \fIfile\fR
510 The variable \fBENVIRON\fR is an array representing the value of the
511 environment. The indices of the array are strings consisting of the names of
512 the environment variables, and the value of each array element is a string
513 consisting of the value of that variable. If the value of an environment
514 variable is considered a \fInumeric string\fR, the array element also has its
517 In all cases where \fBnawk\fR behavior is affected by environment variables
518 (including the environment of any commands that \fBnawk\fR executes via the
519 \fBsystem\fR function or via pipeline redirections with the \fBprint\fR
520 statement, the \fBprintf\fR statement, or the \fBgetline\fR function), the
521 environment used is the environment at the time \fBnawk\fR began executing.
530 A pathname of the current input file. Inside a \fBBEGIN\fR action the value is
531 undefined. Inside an \fBEND\fR action the value is the name of the last input
541 The ordinal number of the current record in the current file. Inside a
542 \fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
543 the number of the last record processed in the last file processed.
552 Input field separator regular expression; a space character by default.
561 The number of fields in the current record. Inside a \fBBEGIN\fR action, the
562 use of \fBNF\fR is undefined unless a \fBgetline\fR function without a
563 \fIvar\fR argument is executed previously. Inside an \fBEND\fR action, \fBNF\fR
564 retains the value it had for the last record read, unless a subsequent,
565 redirected, \fBgetline\fR function without a \fIvar\fR argument is performed
566 prior to entering the \fBEND\fR action.
575 The ordinal number of the current record from the start of input. Inside a
576 \fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
577 the number of the last record processed.
586 The \fBprintf\fR format for converting numbers to strings in output statements
587 \fB"%.6g"\fR by default. The result of the conversion is unspecified if the
588 value of \fBOFMT\fR is not a floating-point format specification.
597 The \fBprint\fR statement output field separator; a space character by default.
606 The \fBprint\fR output record separator; a newline character by default.
615 The length of the string matched by the \fBmatch\fR function.
624 The first character of the string value of \fBRS\fR is the input record
625 separator; a newline character by default. If \fBRS\fR contains more than one
626 character, the results are unspecified. If \fBRS\fR is null, then records are
627 separated by sequences of one or more blank lines. Leading or trailing blank
628 lines do not produce empty records at the beginning or end of input, and the
629 field separator is always newline, no matter what the value of \fBFS\fR.
638 The starting position of the string matched by the \fBmatch\fR function,
639 numbering from 1. This is always equivalent to the return value of the
640 \fBmatch\fR function.
649 The subscript separator string for multi-dimensional arrays. The default value
653 .SS "/usr/xpg4/bin/awk"
656 The following variable is supported for \fB/usr/xpg4/bin/awk\fR only:
663 The \fBprintf\fR format for converting numbers to strings (except for output
664 statements, where \fBOFMT\fR is used). The default is \fB%.6g\fR.
667 .SS "Regular Expressions"
670 The \fBnawk\fR utility makes use of the extended regular expression notation
671 (see \fBregex\fR(5)) except that it allows the use of C-language conventions to
672 escape special characters within the EREs, namely \fB\e\e\fR, \fB\ea\fR,
673 \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR, and those
674 specified in the following table. These escape sequences are recognized both
675 inside and outside bracket expressions. Note that records need not be
676 separated by newline characters and string constants can contain newline
677 characters, so even the \fB\en\fR sequence is valid in \fBnawk\fR EREs. Using
678 a slash character within the regular expression requires escaping as shown in
686 \fBEscape Sequence\fR \fBDescription\fR \fBMeaning\fR
688 \fB\e"\fR Backslash quotation-mark Quotation-mark character
690 \fB\e/\fR Backslash slash Slash character
693 A backslash character followed by the longest sequence of one, two, or three octal-digit characters (01234567). If all of the digits are 0, (that is, representation of the NULL character), the behavior is undefined.
695 The character encoded by the one-, two- or three-digit octal integer. Multi-byte characters require multiple, concatenated escape sequences, including the leading \e for each byte.
699 A backslash character followed by any character not described in this table or special characters (\fB\e\e\fR, \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR).
705 A regular expression can be matched against a specific field or string by using
706 one of the two regular expression matching operators, \fB~\fR and \fB!\|~\fR.
707 These operators interpret their right-hand operand as a regular expression and
708 their left-hand operand as a string. If the regular expression matches the
709 string, the \fB~\fR expression evaluates to the value \fB1\fR, and the
710 \fB!\|~\fR expression evaluates to the value \fB0\fR. If the regular expression
711 does not match the string, the \fB~\fR expression evaluates to the value
712 \fB0\fR, and the \fB!\|~\fR expression evaluates to the value \fB1\fR. If the
713 right-hand operand is any expression other than the lexical token \fBERE\fR,
714 the string value of the expression is interpreted as an extended regular
715 expression, including the escape conventions described above. Notice that these
716 same escape conventions also are applied in the determining the value of a
717 string literal (the lexical token \fBSTRING\fR), and is applied a second time
718 when a string literal is used in this context.
721 When an \fBERE\fR token appears as an expression in any context other than as
722 the right-hand of the \fB~\fR or \fB!\|~\fR operator or as one of the built-in
723 function arguments described below, the value of the resulting expression is
734 The \fIere\fR argument to the \fBgsub,\fR \fBmatch,\fR \fBsub\fR functions, and
735 the \fIfs\fR argument to the \fBsplit\fR function (see \fBString Functions\fR)
736 is interpreted as extended regular expressions. These can be either \fBERE\fR
737 tokens or arbitrary expressions, and are interpreted in the same manner as the
738 right-hand side of the \fB~\fR or \fB!\|~\fR operator.
741 An extended regular expression can be used to separate fields by using the
742 \fB-F\fR \fIERE\fR option or by assigning a string containing the expression to
743 the built-in variable \fBFS\fR. The default value of the \fBFS\fR variable is a
744 single space character. The following describes \fBFS\fR behavior:
748 If \fBFS\fR is a single character:
753 If \fBFS\fR is the space character, skip leading and trailing blank characters;
754 fields are delimited by sets of one or more blank characters.
760 Otherwise, if \fBFS\fR is any other character \fIc\fR, fields are delimited by
761 each single occurrence of \fIc\fR.
767 Otherwise, the string value of \fBFS\fR is considered to be an extended
768 regular expression. Each occurrence of a sequence matching the extended regular
769 expression delimits fields.
773 Except in the \fBgsub\fR, \fBmatch\fR, \fBsplit\fR, and \fBsub\fR built-in
774 functions, regular expression matching is based on input records. That is,
775 record separator characters (the first character of the value of the variable
776 \fBRS\fR, a newline character by default) cannot be embedded in the expression,
777 and no expression matches the record separator character. If the record
778 separator is not a newline character, newline characters embedded in the
779 expression can be matched. In those four built-in functions, regular expression
780 matching are based on text strings. So, any character (including the newline
781 character and the record separator) can be embedded in the pattern and an
782 appropriate pattern matches any character. However, in all \fBnawk\fR regular
783 expression matching, the use of one or more NULL characters in the pattern,
784 input record or text string produces undefined results.
788 A \fIpattern\fR is any valid \fIexpression,\fR a range specified by two
789 expressions separated by comma, or one of the two special patterns \fBBEGIN\fR
791 .SS "Special Patterns"
794 The \fBnawk\fR utility recognizes two special patterns, \fBBEGIN\fR and
795 \fBEND\fR. Each \fBBEGIN\fR pattern is matched once and its associated action
796 executed before the first record of input is read (except possibly by use of
797 the \fBgetline\fR function in a prior \fBBEGIN\fR action) and before command
798 line assignment is done. Each \fBEND\fR pattern is matched once and its
799 associated action executed after the last record of input has been read. These
800 two patterns have associated actions.
803 \fBBEGIN\fR and \fBEND\fR do not combine with other patterns. Multiple
804 \fBBEGIN\fR and \fBEND\fR patterns are allowed. The actions associated with the
805 \fBBEGIN\fR patterns are executed in the order specified in the program, as are
806 the \fBEND\fR actions. An \fBEND\fR pattern can precede a \fBBEGIN\fR pattern
810 If an \fBnawk\fR program consists of only actions with the pattern \fBBEGIN\fR,
811 and the \fBBEGIN\fR action contains no \fBgetline\fR function, \fBnawk\fR exits
812 without reading its input when the last statement in the last \fBBEGIN\fR
813 action is executed. If an \fBnawk\fR program consists of only actions with the
814 pattern \fBEND\fR or only actions with the patterns \fBBEGIN\fR and \fBEND\fR,
815 the input is read before the statements in the \fBEND\fR actions are executed.
816 .SS "Expression Patterns"
819 An expression pattern is evaluated as if it were an expression in a Boolean
820 context. If the result is true, the pattern is considered to match, and the
821 associated action (if any) is executed. If the result is false, the action is
826 A pattern range consists of two expressions separated by a comma. In this case,
827 the action is performed for all records between a match of the first expression
828 and the following match of the second expression, inclusive. At this point, the
829 pattern range can be repeated starting at input records subsequent to the end
830 of the matched range.
834 An action is a sequence of statements. A statement can be one of the following:
838 if ( \fIexpression\fR ) \fIstatement\fR [ else \fIstatement\fR ]
839 while ( \fIexpression\fR ) \fIstatement\fR
840 do \fIstatement\fR while ( \fIexpression\fR )
841 for ( \fIexpression\fR ; \fIexpression\fR ; \fIexpression\fR ) \fIstatement\fR
842 for ( \fIvar\fR in \fIarray\fR ) \fIstatement\fR
843 delete \fIarray\fR[\fIsubscript\fR] #delete an array element
846 { [ \fIstatement\fR ] .\|.\|. }
847 \fIexpression\fR # commonly variable = expression
848 print [ \fIexpression-list\fR ] [ >\fIexpression\fR ]
849 printf format [ ,\fIexpression-list\fR ] [ >\fIexpression\fR ]
850 next # skip remaining patterns on this input line
851 exit [expr] # skip the rest of the input; exit status is expr
858 Any single statement can be replaced by a statement list enclosed in braces.
859 The statements are terminated by newline characters or semicolons, and are
860 executed sequentially in the order that they appear.
863 The \fBnext\fR statement causes all further processing of the current input
864 record to be abandoned. The behavior is undefined if a \fBnext\fR statement
865 appears or is invoked in a \fBBEGIN\fR or \fBEND\fR action.
868 The \fBexit\fR statement invokes all \fBEND\fR actions in the order in which
869 they occur in the program source and then terminate the program without reading
870 further input. An \fBexit\fR statement inside an \fBEND\fR action terminates
871 the program without further execution of \fBEND\fR actions. If an expression
872 is specified in an \fBexit\fR statement, its numeric value is the exit status
873 of \fBnawk\fR, unless subsequent errors are encountered or a subsequent
874 \fBexit\fR statement with an expression is executed.
875 .SS "Output Statements"
878 Both \fBprint\fR and \fBprintf\fR statements write to standard output by
879 default. The output is written to the location specified by
880 \fIoutput_redirection\fR if one is supplied, as follows:
884 \fB>\fR \fIexpression\fR\fB>>\fR \fIexpression\fR\fB|\fR \fIexpression\fR
890 In all cases, the \fIexpression\fR is evaluated to produce a string that is
891 used as a full pathname to write into (for \fB>\fR or \fB>>\fR) or as a command
892 to be executed (for \fB|\fR). Using the first two forms, if the file of that
893 name is not currently open, it is opened, creating it if necessary and using
894 the first form, truncating the file. The output then is appended to the file.
895 As long as the file remains open, subsequent calls in which \fIexpression\fR
896 evaluates to the same string value simply appends output to the file. The file
897 remains open until the \fBclose\fR function, which is called with an expression
898 that evaluates to the same string value.
901 The third form writes output onto a stream piped to the input of a command. The
902 stream is created if no stream is currently open with the value of
903 \fIexpression\fR as its command name. The stream created is equivalent to one
904 created by a call to the \fBpopen\fR(3C) function with the value of
905 \fIexpression\fR as the \fIcommand\fR argument and a value of \fBw\fR as the
906 \fImode\fR argument. As long as the stream remains open, subsequent calls in
907 which \fIexpression\fR evaluates to the same string value writes output to the
908 existing stream. The stream remains open until the \fBclose\fR function is
909 called with an expression that evaluates to the same string value. At that
910 time, the stream is closed as if by a call to the \fBpclose\fR function.
913 These output statements take a comma-separated list of \fIexpression\fR \fIs\fR
914 referred in the grammar by the non-terminal symbols \fBexpr_list,\fR
915 \fBprint_expr_list\fR or \fBprint_expr_list_opt.\fR This list is referred to
916 here as the \fIexpression list\fR, and each member is referred to as an
917 \fIexpression argument\fR.
920 The \fBprint\fR statement writes the value of each expression argument onto the
921 indicated output stream separated by the current output field separator (see
922 variable \fBOFS\fR above), and terminated by the output record separator (see
923 variable \fBORS\fR above). All expression arguments is taken as strings, being
924 converted if necessary; with the exception that the \fBprintf\fR format in
925 \fBOFMT\fR is used instead of the value in \fBCONVFMT\fR. An empty expression
926 list stands for the whole input record \fB(\fR$0\fB)\fR.
929 The \fBprintf\fR statement produces output based on a notation similar to the
930 File Format Notation used to describe file formats in this document Output is
931 produced as specified with the first expression argument as the string
932 \fBformat\fR and subsequent expression arguments as the strings \fBarg1\fR to
933 \fBargn,\fR inclusive, with the following exceptions:
937 The \fIformat\fR is an actual character string rather than a graphical
938 representation. Therefore, it cannot contain empty character positions. The
939 space character in the \fIformat\fR string, in any context other than a
940 \fIflag\fR of a conversion specification, is treated as an ordinary character
941 that is copied to the output.
946 If the character set contains a Delta character and that character appears
947 in the \fIformat\fR string, it is treated as an ordinary character that is
948 copied to the output.
953 The \fIescape sequences\fR beginning with a backslash character is treated
954 as sequences of ordinary characters that are copied to the output. Note that
955 these same sequences is interpreted lexically by \fBnawk\fR when they appear in
956 literal strings, but they is not treated specially by the \fBprintf\fR
962 A \fIfield width\fR or \fIprecision\fR can be specified as the \fB*\fR
963 character instead of a digit string. In this case the next argument from the
964 expression list is fetched and its numeric value taken as the field width or
970 The implementation does not precede or follow output from the \fBd\fR or
971 \fBu\fR conversion specifications with blank characters not specified by the
977 The implementation does not precede output from the \fBo\fR conversion
978 specification with leading zeros not specified by the \fIformat\fR string.
983 For the \fBc\fR conversion specification: if the argument has a numeric
984 value, the character whose encoding is that value is output. If the value is
985 zero or is not the encoding of any character in the character set, the behavior
986 is undefined. If the argument does not have a numeric value, the first
987 character of the string value is output; if the string does not contain any
988 characters the behavior is undefined.
993 For each conversion specification that consumes an argument, the next
994 expression argument is evaluated. With the exception of the \fBc\fR conversion,
995 the value is converted to the appropriate type for the conversion
1001 If there are insufficient expression arguments to satisfy all the conversion
1002 specifications in the \fIformat\fR string, the behavior is undefined.
1007 If any character sequence in the \fIformat\fR string begins with a %
1008 character, but does not form a valid conversion specification, the behavior is
1013 Both \fBprint\fR and \fBprintf\fR can output at least \fB{LINE_MAX}\fR bytes.
1017 The \fBnawk\fR language has a variety of built-in functions: arithmetic,
1018 string, input/output and general.
1019 .SS "Arithmetic Functions"
1022 The arithmetic functions, except for \fBint\fR, are based on the \fBISO\fR
1023 \fBC\fR standard. The behavior is undefined in cases where the \fBISO\fR
1024 \fBC\fR standard specifies that an error be returned or that the behavior is
1025 undefined. Although the grammar permits built-in functions to appear with no
1026 arguments or parentheses, unless the argument or parentheses are indicated as
1027 optional in the following list (by displaying them within the \fB[ ]\fR
1028 brackets), such use is undefined.
1032 \fB\fBatan2(\fR\fIy\fR,\fIx\fR\fB)\fR\fR
1035 Return arctangent of \fIy\fR/\fIx\fR.
1041 \fB\fBcos\fR(\fIx\fR)\fR
1044 Return cosine of \fIx,\fR where \fIx\fR is in radians.
1050 \fB\fBsin\fR(\fIx\fR)\fR
1053 Return sine of \fIx,\fR where \fIx\fR is in radians.
1059 \fB\fBexp\fR(\fIx\fR)\fR
1062 Return the exponential function of \fIx\fR.
1068 \fB\fBlog\fR(\fIx\fR)\fR
1071 Return the natural logarithm of \fIx\fR.
1077 \fB\fBsqrt\fR(\fIx\fR)\fR
1080 Return the square root of \fIx\fR.
1086 \fB\fBint\fR(\fIx\fR)\fR
1089 Truncate its argument to an integer. It is truncated toward 0 when \fIx\fR > 0.
1098 Return a random number \fIn\fR, such that 0 \(<= \fIn\fR < 1.
1104 \fB\fBsrand\fR([\fBexpr\fR])\fR
1107 Set the seed value for \fBrand\fR to \fIexpr\fR or use the time of day if
1108 \fIexpr\fR is omitted. The previous seed value is returned.
1111 .SS "String Functions"
1114 The string functions in the following list shall be supported. Although the
1115 grammar permits built-in functions to appear with no arguments or parentheses,
1116 unless the argument or parentheses are indicated as optional in the following
1117 list (by displaying them within the \fB[ ]\fR brackets), such use is undefined.
1121 \fB\fBgsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
1125 Behave like \fBsub\fR (see below), except that it replaces all occurrences of
1126 the regular expression (like the \fBed\fR utility global substitute) in
1127 \fB$0\fR or in the \fIin\fR argument, when specified.
1133 \fB\fBindex\fR(\fIs\fR,\fIt\fR)\fR
1137 Return the position, in characters, numbering from 1, in string \fIs\fR where
1138 string \fIt\fR first occurs, or zero if it does not occur at all.
1144 \fB\fBlength\fR[([\fIs\fR])]\fR
1148 Return the length, in characters, of its argument taken as a string, or of the
1149 whole record, \fB$0\fR, if there is no argument.
1155 \fB\fBmatch\fR(\fIs\fR,\fIere\fR)\fR
1159 Return the position, in characters, numbering from 1, in string \fIs\fR where
1160 the extended regular expression \fIere\fR occurs, or zero if it does not occur
1161 at all. \fBRSTART\fR is set to the starting position (which is the same as the
1162 returned value), zero if no match is found; \fBRLENGTH\fR is set to the length
1163 of the matched string, \(mi1 if no match is found.
1169 \fB\fBsplit\fR(\fIs\fR,\fIa\fR[,\|\fIfs\fR])\fR
1173 Split the string \fIs\fR into array elements \fIa\fR[1], \fIa\fR[2],
1174 \fB\&...,\fR \fIa\fR[\fIn\fR], and return \fIn\fR. The separation is done with
1175 the extended regular expression \fIfs\fR or with the field separator \fBFS\fR
1176 if \fIfs\fR is not given. Each array element has a string value when created.
1177 If the string assigned to any array element, with any occurrence of the
1178 decimal-point character from the current locale changed to a period character,
1179 would be considered a \fInumeric string\fR; the array element also has the
1180 numeric value of the \fInumeric string\fR. The effect of a null string as the
1181 value of \fIfs\fR is unspecified.
1187 \fB\fBsprintf\fR(\fBfmt\fR,\fIexpr\fR,\fIexpr\fR,\fB\&...\fR)\fR
1191 Format the expressions according to the \fBprintf\fR format given by \fIfmt\fR
1192 and return the resulting string.
1198 \fB\fBsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
1202 Substitute the string \fIrepl\fR in place of the first instance of the extended
1203 regular expression \fBERE\fR in string in and return the number of
1204 substitutions. An ampersand ( \fB&\fR ) appearing in the string \fIrepl\fR is
1205 replaced by the string from in that matches the regular expression. An
1206 ampersand preceded with a backslash ( \fB\e\fR ) is interpreted as the literal
1207 ampersand character. An occurrence of two consecutive backslashes is
1208 interpreted as just a single literal backslash character. Any other occurrence
1209 of a backslash (for example, preceding any other character) is treated as a
1210 literal backslash character. If \fIrepl\fR is a string literal, the handling of
1211 the ampersand character occurs after any lexical processing, including any
1212 lexical backslash escape sequence processing. If \fBin\fR is specified and it
1213 is not an \fBlvalue\fR the behavior is undefined. If in is omitted, \fBnawk\fR
1214 uses the current record (\fB$0\fR) in its place.
1220 \fB\fBsubstr\fR(\fIs\fR,\fIm\fR[,\|\fIn\fR])\fR
1224 Return the at most \fIn\fR-character substring of \fIs\fR that begins at
1225 position \fIm,\fR numbering from 1. If \fIn\fR is missing, the length of the
1226 substring is limited by the length of the string \fIs\fR.
1232 \fB\fBtolower\fR(\fIs\fR)\fR
1236 Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
1237 an upper-case letter specified to have a \fBtolower\fR mapping by the
1238 \fBLC_CTYPE\fR category of the current locale is replaced in the returned
1239 string by the lower-case letter specified by the mapping. Other characters in
1240 \fIs\fR are unchanged in the returned string.
1246 \fB\fBtoupper\fR(\fIs\fR)\fR
1250 Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
1251 a lower-case letter specified to have a \fBtoupper\fR mapping by the
1252 \fBLC_CTYPE\fR category of the current locale is replaced in the returned
1253 string by the upper-case letter specified by the mapping. Other characters in
1254 \fIs\fR are unchanged in the returned string.
1259 All of the preceding functions that take \fIERE\fR as a parameter expect a
1260 pattern or a string valued expression that is a regular expression as defined
1262 .SS "Input/Output and General Functions"
1265 The input/output and general functions are:
1269 \fB\fBclose(\fR\fIexpression\fR)\fR
1272 Close the file or pipe opened by a \fBprint\fR or \fBprintf\fR statement or a
1273 call to \fBgetline\fR with the same string-valued \fIexpression\fR. If the
1274 close was successful, the function returns \fB0\fR; otherwise, it returns
1281 \fB\fIexpression\fR|\fBgetline\fR[\fIvar\fR]\fR
1284 Read a record of input from a stream piped from the output of a command. The
1285 stream is created if no stream is currently open with the value of
1286 \fIexpression\fR as its command name. The stream created is equivalent to one
1287 created by a call to the \fBpopen\fR function with the value of
1288 \fIexpression\fR as the \fIcommand\fR argument and a value of \fBr\fR as the
1289 \fImode\fR argument. As long as the stream remains open, subsequent calls in
1290 which \fIexpression\fR evaluates to the same string value reads subsequent
1291 records from the file. The stream remains open until the \fBclose\fR function
1292 is called with an expression that evaluates to the same string value. At that
1293 time, the stream is closed as if by a call to the \fBpclose\fR function. If
1294 \fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise, \fIvar\fR is
1297 The \fBgetline\fR operator can form ambiguous constructs when there are
1298 operators that are not in parentheses (including concatenate) to the left of
1299 the \fB|\fR (to the beginning of the expression containing \fBgetline\fR). In
1300 the context of the \fB$\fR operator, \fB|\fR behaves as if it had a lower
1301 precedence than \fB$\fR. The result of evaluating other operators is
1302 unspecified, and all such uses of portable applications must be put in
1303 parentheses properly.
1312 Set \fB$0\fR to the next input record from the current input file. This form of
1313 \fBgetline\fR sets the \fBNF\fR, \fBNR\fR, and \fBFNR\fR variables.
1319 \fB\fBgetline\fR \fIvar\fR\fR
1322 Set variable \fIvar\fR to the next input record from the current input file.
1323 This form of \fBgetline\fR sets the \fBFNR\fR and \fBNR\fR variables.
1329 \fB\fBgetline\fR [\fIvar\fR] \fB<\fR \fIexpression\fR\fR
1332 Read the next record of input from a named file. The \fIexpression\fR is
1333 evaluated to produce a string that is used as a full pathname. If the file of
1334 that name is not currently open, it is opened. As long as the stream remains
1335 open, subsequent calls in which \fIexpression\fR evaluates to the same string
1336 value reads subsequent records from the file. The file remains open until the
1337 \fBclose\fR function is called with an expression that evaluates to the same
1338 string value. If \fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise,
1341 The \fBgetline\fR operator can form ambiguous constructs when there are binary
1342 operators that are not in parentheses (including concatenate) to the right of
1343 the \fB<\fR (up to the end of the expression containing the \fBgetline\fR). The
1344 result of evaluating such a construct is unspecified, and all such uses of
1345 portable applications must be put in parentheses properly.
1351 \fB\fBsystem\fR(\fIexpression\fR)\fR
1354 Execute the command given by \fIexpression\fR in a manner equivalent to the
1355 \fBsystem\fR(3C) function and return the exit status of the command.
1360 All forms of \fBgetline\fR return \fB1\fR for successful input, \fB0\fR for end
1361 of file, and \fB\(mi1\fR for an error.
1364 Where strings are used as the name of a file or pipeline, the strings must be
1365 textually identical. The terminology ``same string value'' implies that
1366 ``equivalent strings'', even those that differ only by space characters,
1367 represent different files.
1368 .SS "User-defined Functions"
1371 The \fBnawk\fR language also provides user-defined functions. Such functions
1376 \fBfunction\fR \fIname\fR(\fIargs\fR,\|.\|.\|.) { \fIstatements\fR }
1382 A function can be referred to anywhere in an \fBnawk\fR program; in particular,
1383 its use can precede its definition. The scope of a function is global.
1386 Function arguments can be either scalars or arrays; the behavior is undefined
1387 if an array name is passed as an argument that the function uses as a scalar,
1388 or if a scalar expression is passed as an argument that the function uses as an
1389 array. Function arguments are passed by value if scalar and by reference if
1390 array name. Argument names are local to the function; all other variable names
1391 are global. The same name is not used as both an argument name and as the name
1392 of a function or a special \fBnawk\fR variable. The same name must not be used
1393 both as a variable name with global scope and as the name of a function. The
1394 same name must not be used within the same scope both as a scalar variable and
1398 The number of parameters in the function definition need not match the number
1399 of parameters in the function call. Excess formal parameters can be used as
1400 local variables. If fewer arguments are supplied in a function call than are in
1401 the function definition, the extra parameters that are used in the function
1402 body as scalars are initialized with a string value of the null string and a
1403 numeric value of zero, and the extra parameters that are used in the function
1404 body as arrays are initialized as empty arrays. If more arguments are supplied
1405 in a function call than are in the function definition, the behavior is
1409 When invoking a function, no white space can be placed between the function
1410 name and the opening parenthesis. Function calls can be nested and recursive
1411 calls can be made upon functions. Upon return from any nested or recursive
1412 function call, the values of all of the calling function's parameters are
1413 unchanged, except for array parameters passed by reference. The \fBreturn\fR
1414 statement can be used to return a value. If a \fBreturn\fR statement appears
1415 outside of a function definition, the behavior is undefined.
1418 In the function definition, newline characters are optional before the opening
1419 brace and after the closing brace. Function definitions can appear anywhere in
1420 the program where a \fIpattern-action\fR pair is allowed.
1424 The \fBindex\fR, \fBlength\fR, \fBmatch\fR, and \fBsubstr\fR functions should
1425 not be confused with similar functions in the \fBISO C\fR standard; the
1426 \fBnawk\fR versions deal with characters, while the \fBISO C\fR standard deals
1430 Because the concatenation operation is represented by adjacent expressions
1431 rather than an explicit operator, it is often necessary to use parentheses to
1432 enforce the proper evaluation precedence.
1435 See \fBlargefile\fR(5) for the description of the behavior of \fBnawk\fR when
1436 encountering files greater than or equal to 2 Gbyte (2^31 bytes).
1440 The \fBnawk\fR program specified in the command line is most easily specified
1441 within single-quotes (for example, \fB\&'\fR\fIprogram\fR\fB\&'\fR) for
1442 applications using \fBsh\fR, because \fBnawk\fR programs commonly contain
1443 characters that are special to the shell, including double-quotes. In the cases
1444 where a \fBnawk\fR program contains single-quote characters, it is usually
1445 easiest to specify most of the program as strings within single-quotes
1446 concatenated by the shell with quoted single-quote characters. For example:
1450 nawk '/'\e''/ { print "quote:", $0 }'
1456 prints all lines from the standard input containing a single-quote character,
1457 prefixed with \fBquote:\fR.
1460 The following are examples of simple \fBnawk\fR programs:
1462 \fBExample 1 \fRWrite to the standard output all input lines for which field 3
1473 \fBExample 2 \fRWrite every tenth line:
1477 \fB(NR % 10) == 0\fR
1483 \fBExample 3 \fRWrite any line with a substring matching the regular
1488 \fB/(G|D)(2[0-9][[:alpha:]]*)/\fR
1494 \fBExample 4 \fRPrint any line with a substring containing a G or D, followed
1495 by a sequence of digits and characters:
1498 This example uses character classes \fBdigit\fR and \fBalpha\fR to match
1499 language-independent digit and alphabetic characters, respectively.
1504 \fB/(G|D)([[:digit:][:alpha:]]*)/\fR
1510 \fBExample 5 \fRWrite any line in which the second field matches the regular
1511 expression and the fourth field does not:
1515 \fB$2 ~ /xyz/ && $4 !~ /xyz/\fR
1521 \fBExample 6 \fRWrite any line in which the second field contains a backslash:
1531 \fBExample 7 \fRWrite any line in which the second field contains a backslash
1535 Notice that backslash escapes are interpreted twice, once in lexical processing
1536 of the string and once in processing the regular expression.
1541 \fB$2 ~ "\e\e\e\e"\fR
1547 \fBExample 8 \fRWrite the second to the last and the last field in each line,
1548 separating the fields by a colon:
1552 \fB{OFS=":";print $(NF-1), $NF}\fR
1558 \fBExample 9 \fRWrite the line number and number of fields in each line:
1561 The three strings representing the line number, the colon and the number of
1562 fields are concatenated and that string is written to standard output.
1567 \fB{print NR ":" NF}\fR
1573 \fBExample 10 \fRWrite lines longer than 72 characters:
1577 \fB{length($0) > 72}\fR
1583 \fBExample 11 \fRWrite first two fields in opposite order separated by the OFS:
1587 \fB{ print $2, $1 }\fR
1593 \fBExample 12 \fRSame, with input fields separated by comma or space and tab
1594 characters, or both:
1598 \fBBEGIN { FS = ",[\et]*|[\et]+" }
1605 \fBExample 13 \fRAdd up first column, print sum and average:
1610 END {print "sum is ", s, " average is", s/NR}\fR
1616 \fBExample 14 \fRWrite fields in reverse order, one per line (many lines out
1621 \fB{ for (i = NF; i > 0; --i) print $i }\fR
1627 \fBExample 15 \fRWrite all lines between occurrences of the strings "start" and
1632 \fB/start/, /stop/\fR
1638 \fBExample 16 \fRWrite all lines whose first field is different from the
1643 \fB$1 != prev { print; prev = $1 }\fR
1649 \fBExample 17 \fRSimulate the echo command:
1654 for (i = 1; i < ARGC; ++i)
1655 printf "%s%s", ARGV[i], i==ARGC-1?"\en":""
1662 \fBExample 18 \fRWrite the path prefixes contained in the PATH environment
1663 variable, one per line:
1668 n = split (ENVIRON["PATH"], path, ":")
1669 for (i = 1; i <= n; ++i)
1677 \fBExample 19 \fRPrint the file "input", filling in page numbers starting at 5:
1680 If there is a file named \fBinput\fR containing page headers of the form
1691 and a file named \fBprogram\fR that contains
1703 then the command line
1708 \fBnawk -f program n=5 input\fR
1715 prints the file \fBinput\fR, filling in page numbers starting at 5.
1717 .SH ENVIRONMENT VARIABLES
1720 See \fBenviron\fR(5) for descriptions of the following environment variables
1721 that affect execution: \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and
1726 \fB\fBLC_NUMERIC\fR\fR
1729 Determine the radix character used when interpreting numeric input, performing
1730 conversions between numeric and string values and formatting numeric output.
1731 Regardless of locale, the period character (the decimal-point character of the
1732 POSIX locale) is the decimal-point character recognized in processing \fBawk\fR
1733 programs (including assignments in command-line arguments).
1739 The following exit values are returned:
1746 All input files were processed successfully.
1760 The exit status can be altered within the program by using an \fBexit\fR
1766 \fBawk\fR(1), \fBed\fR(1), \fBegrep\fR(1), \fBgrep\fR(1), \fBlex\fR(1),
1767 \fBsed\fR(1), \fBpopen\fR(3C), \fBprintf\fR(3C), \fBsystem\fR(3C),
1768 \fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5),
1772 Aho, A. V., B. W. Kernighan, and P. J. Weinberger, \fIThe AWK Programming
1773 Language\fR, Addison-Wesley, 1988.
1777 If any \fIfile\fR operand is specified and the named file cannot be accessed,
1778 \fBnawk\fR writes a diagnostic message to standard error and terminate without
1782 If the program specified by either the \fIprogram\fR operand or a
1783 \fIprogfile\fR operand is not a valid \fBnawk\fR program (as specified in
1784 \fBEXTENDED DESCRIPTION\fR), the behavior is undefined.
1788 Input white space is not preserved on output if fields are involved.
1791 There are no explicit conversions between numbers and strings. To force an
1792 expression to be treated as a number add 0 to it; to force it to be treated as
1793 a string concatenate the null string (\fB""\fR) to it.