Print this page
XXX Remove nawk(1)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/man/man1/nawk.1
+++ new/usr/src/man/man1/nawk.1
1 1 '\" te
2 2 .\" Copyright 1989 AT&T
3 3 .\" Copyright 1992, X/Open Company Limited All Rights Reserved
4 4 .\" Portions Copyright (c) 2005, 2006 Sun Microsystems, Inc. All Rights Reserved
5 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 6 .\" http://www.opengroup.org/bookstore/.
7 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.
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
8 8 .\" This notice shall appear on any product containing this material.
9 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 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 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 12 .TH NAWK 1 "May 24, 2006"
13 13 .SH NAME
14 14 nawk \- pattern scanning and processing language
15 15 .SH SYNOPSIS
16 16 .LP
17 17 .nf
18 -\fB/usr/bin/nawk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR] \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
19 - [\fIargument\fR]...
20 -.fi
21 -
22 -.LP
23 -.nf
24 18 \fB/usr/xpg4/bin/awk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR]... \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
25 19 [\fIargument\fR]...
26 20 .fi
27 21
28 22 .SH DESCRIPTION
29 23 .sp
30 24 .LP
31 -The \fB/usr/bin/nawk\fR and \fB/usr/xpg4/bin/awk\fR utilities execute
25 +The \fB/usr/xpg4/bin/awk\fR utility executes
32 26 \fIprogram\fRs written in the \fBnawk\fR programming language, which is
33 27 specialized for textual data manipulation. A \fBnawk\fR \fIprogram\fR is a
34 28 sequence of patterns and corresponding actions. The string specifying
35 29 \fIprogram\fR must be enclosed in single quotes (') to protect it from
36 30 interpretation by the shell. The sequence of pattern - action statements can be
37 31 specified in the command line as \fIprogram\fR or in one, or more, file(s)
38 32 specified by the \fB-f\fR\fIprogfile\fR option. When input is read that matches
39 33 a pattern, the action associated with the pattern is performed.
40 34 .sp
41 35 .LP
42 36 Input is interpreted as a sequence of records. By default, a record is a line,
43 37 but this can be changed by using the \fBRS\fR built-in variable. Each record of
44 38 input is matched to each pattern in the \fIprogram\fR. For each pattern
45 39 matched, the associated action is executed.
46 40 .sp
47 41 .LP
48 42 The \fBnawk\fR utility interprets each input record as a sequence of fields
49 43 where, by default, a field is a string of non-blank characters. This default
50 44 white-space field delimiter (blanks and/or tabs) can be changed by using the
51 45 \fBFS\fR built-in variable or the \fB-F\fR\fIERE\fR option. The \fBnawk\fR
52 46 utility denotes the first field in a record \fB$1\fR, the second \fB$2\fR, and
53 47 so forth. The symbol \fB$0\fR refers to the entire record; setting any other
54 48 field causes the reevaluation of \fB$0\fR. Assigning to \fB$0\fR resets the
55 49 values of all fields and the \fBNF\fR built-in variable.
56 50 .SH OPTIONS
57 51 .sp
58 52 .LP
59 53 The following options are supported:
60 54 .sp
61 55 .ne 2
62 56 .na
63 57 \fB\fB-F\fR \fIERE\fR\fR
64 58 .ad
65 59 .RS 17n
66 60 Define the input field separator to be the extended regular expression
67 61 \fIERE\fR, before any input is read (can be a character).
68 62 .RE
69 63
70 64 .sp
71 65 .ne 2
72 66 .na
73 67 \fB\fB-f\fR \fIprogfile\fR\fR
74 68 .ad
75 69 .RS 17n
76 70 Specifies the pathname of the file \fIprogfile\fR containing a \fBnawk\fR
77 71 program. If multiple instances of this option are specified, the concatenation
78 72 of the files specified as \fIprogfile\fR in the order specified is the
79 73 \fBnawk\fR program. The \fBnawk\fR program can alternatively be specified in
80 74 the command line as a single argument.
81 75 .RE
82 76
83 77 .sp
84 78 .ne 2
85 79 .na
86 80 \fB\fB-v\fR \fIassignment\fR\fR
87 81 .ad
88 82 .RS 17n
89 83 The \fIassignment\fR argument must be in the same form as an \fIassignment\fR
90 84 operand. The assignment is of the form \fIvar=value\fR, where \fIvar\fR is the
91 85 name of one of the variables described below. The specified assignment occurs
92 86 before executing the \fBnawk\fR program, including the actions associated with
93 87 \fBBEGIN\fR patterns (if any). Multiple occurrences of this option can be
94 88 specified.
95 89 .RE
96 90
97 91 .SH OPERANDS
98 92 .sp
99 93 .LP
100 94 The following operands are supported:
101 95 .sp
102 96 .ne 2
103 97 .na
104 98 \fB\fIprogram\fR\fR
105 99 .ad
106 100 .RS 12n
107 101 If no \fB-f\fR option is specified, the first operand to \fBnawk\fR is the text
108 102 of the \fBnawk\fR program. The application supplies the \fIprogram\fR operand
109 103 as a single argument to \fBnawk.\fR If the text does not end in a newline
110 104 character, \fBnawk\fR interprets the text as if it did.
111 105 .RE
112 106
113 107 .sp
114 108 .ne 2
115 109 .na
116 110 \fB\fIargument\fR\fR
117 111 .ad
118 112 .RS 12n
119 113 Either of the following two types of \fIargument\fR can be intermixed:
120 114 .sp
121 115 .ne 2
122 116 .na
123 117 \fB\fIfile\fR\fR
124 118 .ad
125 119 .RS 14n
126 120 A pathname of a file that contains the input to be read, which is matched
127 121 against the set of patterns in the program. If no \fIfile\fR operands are
128 122 specified, or if a \fIfile\fR operand is \fB\(mi\fR, the standard input is
129 123 used.
130 124 .RE
131 125
132 126 .sp
133 127 .ne 2
134 128 .na
135 129 \fB\fIassignment\fR\fR
136 130 .ad
137 131 .RS 14n
138 132 An operand that begins with an underscore or alphabetic character from the
139 133 portable character set, followed by a sequence of underscores, digits and
140 134 alphabetics from the portable character set, followed by the \fB=\fR character
141 135 specifies a variable assignment rather than a pathname. The characters before
142 136 the \fB=\fR represent the name of a \fBnawk\fR variable. If that name is a
143 137 \fBnawk\fR reserved word, the behavior is undefined. The characters following
144 138 the equal sign is interpreted as if they appeared in the \fBnawk\fR program
145 139 preceded and followed by a double-quote (\fB"\fR) character, as a \fBSTRING\fR
146 140 token , except that if the last character is an unescaped backslash, it is
147 141 interpreted as a literal backslash rather than as the first character of the
148 142 sequence \fB\e\fR\&.. The variable is assigned the value of that \fBSTRING\fR
149 143 token. If the value is considered a \fInumeric\fRstring\fI,\fR the variable is
150 144 assigned its numeric value. Each such variable assignment is performed just
151 145 before the processing of the following \fIfile\fR, if any. Thus, an assignment
152 146 before the first \fBfile\fR argument is executed after the \fBBEGIN\fR actions
153 147 (if any), while an assignment after the last \fIfile\fR argument is executed
154 148 before the \fBEND\fR actions (if any). If there are no \fIfile\fR arguments,
155 149 assignments are executed before processing the standard input.
156 150 .RE
157 151
158 152 .RE
159 153
160 154 .SH INPUT FILES
161 155 .sp
162 156 .LP
163 157 Input files to the \fBnawk\fR program from any of the following sources:
164 158 .RS +4
165 159 .TP
166 160 .ie t \(bu
167 161 .el o
168 162 any \fIfile\fR operands or their equivalents, achieved by modifying the
169 163 \fBnawk\fR variables \fBARGV\fR and \fBARGC\fR
170 164 .RE
171 165 .RS +4
172 166 .TP
173 167 .ie t \(bu
174 168 .el o
175 169 standard input in the absence of any \fIfile\fR operands
176 170 .RE
177 171 .RS +4
178 172 .TP
179 173 .ie t \(bu
180 174 .el o
181 175 arguments to the \fBgetline\fR function
182 176 .RE
183 177 .sp
184 178 .LP
185 179 must be text files. Whether the variable \fBRS\fR is set to a value other than
186 180 a newline character or not, for these files, implementations support records
187 181 terminated with the specified separator up to \fB{LINE_MAX}\fR bytes and can
188 182 support longer records.
189 183 .sp
190 184 .LP
191 185 If \fB-\fR\fBf\fR \fIprogfile\fR is specified, the files named by each of the
192 186 \fIprogfile\fR option-arguments must be text files containing an \fBnawk\fR
193 187 program.
194 188 .sp
195 189 .LP
196 190 The standard input are used only if no \fIfile\fR operands are specified, or if
197 191 a \fIfile\fR operand is \fB\(mi\fR\&.
198 192 .SH EXTENDED DESCRIPTION
199 193 .sp
200 194 .LP
201 195 A \fBnawk\fR program is composed of pairs of the form:
202 196 .sp
203 197 .in +2
204 198 .nf
205 199 pattern { \fIaction\fR }
206 200 .fi
207 201 .in -2
208 202
209 203 .sp
210 204 .LP
211 205 Either the pattern or the action (including the enclosing brace characters) can
212 206 be omitted. Pattern-action statements are separated by a semicolon or by a
213 207 newline.
214 208 .sp
215 209 .LP
216 210 A missing pattern matches any record of input, and a missing action is
217 211 equivalent to an action that writes the matched record of input to standard
218 212 output.
219 213 .sp
220 214 .LP
221 215 Execution of the \fBnawk\fR program starts by first executing the actions
222 216 associated with all \fBBEGIN\fR patterns in the order they occur in the
223 217 program. Then each \fIfile\fR operand (or standard input if no files were
224 218 specified) is processed by reading data from the file until a record separator
225 219 is seen (a newline character by default), splitting the current record into
226 220 fields using the current value of \fBFS\fR, evaluating each pattern in the
227 221 program in the order of occurrence, and executing the action associated with
228 222 each pattern that matches the current record. The action for a matching pattern
229 223 is executed before evaluating subsequent patterns. Last, the actions associated
230 224 with all \fBEND\fR patterns is executed in the order they occur in the program.
231 225 .SS "Expressions in nawk"
232 226 .sp
233 227 .LP
234 228 Expressions describe computations used in \fIpatterns\fR and \fIactions\fR. In
235 229 the following table, valid expression operations are given in groups from
236 230 highest precedence first to lowest precedence last, with equal-precedence
237 231 operators grouped between horizontal lines. In expression evaluation, where the
238 232 grammar is formally ambiguous, higher precedence operators are evaluated before
239 233 lower precedence operators. In this table \fIexpr,\fR \fIexpr1,\fR
240 234 \fIexpr2,\fR and \fIexpr3\fR represent any expression, while \fIlvalue\fR
241 235 represents any entity that can be assigned to (that is, on the left side of an
242 236 assignment operator).
243 237 .sp
244 238
245 239 .sp
246 240 .TS
247 241 c c c c
248 242 l l l l .
249 243 \fBSyntax\fR \fBName\fR \fBType of Result\fR \fBAssociativity\fR
250 244 _
251 245 ( \fIexpr\fR ) Grouping type of \fIexpr\fR n/a
252 246 _
253 247 $\fIexpr\fR Field reference string n/a
254 248 _
255 249 ++ \fIlvalue\fR Pre-increment numeric n/a
256 250 \(mi\(mi\fIlvalue\fR Pre-decrement numeric n/a
257 251 \fIlvalue\fR ++ Post-increment numeric n/a
258 252 \fIlvalue\fR \(mi\(mi Post-decrement numeric n/a
259 253 _
260 254 \fIexpr\fR ^ \fIexpr\fR Exponentiation numeric right
261 255 _
262 256 ! \fIexpr\fR Logical not numeric n/a
263 257 + \fIexpr\fR Unary plus numeric n/a
264 258 \(mi \fIexpr\fR Unary minus numeric n/a
265 259 _
266 260 \fIexpr\fR * \fIexpr\fR Multiplication numeric left
267 261 \fIexpr\fR / \fIexpr\fR Division numeric left
268 262 \fIexpr\fR % \fIexpr\fR Modulus numeric left
269 263 _
270 264 \fIexpr\fR + \fIexpr\fR Addition numeric left
271 265 \fIexpr\fR \(mi \fIexpr\fR Subtraction numeric left
272 266 _
273 267 \fIexpr\fR \fIexpr\fR String concatenation string left
274 268 _
275 269 \fIexpr\fR < \fIexpr\fR Less than numeric none
276 270 \fIexpr\fR <= \fIexpr\fR Less than or equal to numeric none
277 271 \fIexpr\fR != \fIexpr\fR Not equal to numeric none
278 272 \fIexpr\fR == \fIexpr\fR Equal to numeric none
279 273 \fIexpr\fR > \fIexpr\fR Greater than numeric none
280 274 \fIexpr\fR >= \fIexpr\fR Greater than or equal to numeric none
281 275 _
282 276 \fIexpr\fR ~ \fIexpr\fR ERE match numeric none
283 277 \fIexpr\fR !~ \fIexpr\fR ERE non-match numeric none
284 278 _
285 279 \fIexpr\fR in array Array membership numeric left
286 280 ( \fIindex\fR ) in Multi-dimension array numeric left
287 281 \fIarray\fR membership
288 282 _
289 283 \fBexpr\fR && \fIexpr\fR Logical AND numeric left
290 284 _
291 285 \fBexpr\fR |\|| \fIexpr\fR Logical OR numeric left
292 286 _
293 287 \fIexpr1\fR ? \fIexpr2\fR Conditional expression type of selected right
294 288 : \fIexpr3\fR \fIexpr2\fR or \fIexpr3\fR
295 289 _
296 290 \fIlvalue\fR ^= \fIexpr\fR Exponentiation numeric right
297 291 assignment
298 292 \fIlvalue\fR %= \fIexpr\fR Modulus assignment numeric right
299 293 \fIlvalue\fR *= \fIexpr\fR Multiplication numeric right
300 294 assignment
301 295 \fIlvalue\fR /= \fIexpr\fR Division assignment numeric right
302 296 \fIlvalue\fR += \fIexpr\fR Addition assignment numeric right
303 297 \fIlvalue\fR \(mi= \fIexpr\fR Subtraction assignment numeric right
304 298 \fIlvalue\fR = \fIexpr\fR Assignment type of \fIexpr\fR right
305 299 .TE
306 300
307 301 .sp
308 302 .LP
309 303 Each expression has either a string value, a numeric value or both. Except as
310 304 stated for specific contexts, the value of an expression is implicitly
311 305 converted to the type needed for the context in which it is used. A string
312 306 value is converted to a numeric value by the equivalent of the following calls:
313 307 .sp
314 308 .in +2
315 309 .nf
316 310 setlocale(LC_NUMERIC, "");
317 311 \fInumeric_value\fR = atof(\fIstring_value\fR);
318 312 .fi
319 313 .in -2
320 314
321 315 .sp
322 316 .LP
323 317 A numeric value that is exactly equal to the value of an integer is converted
324 318 to a string by the equivalent of a call to the \fBsprintf\fR function with the
325 319 string \fB%d\fR as the \fBfmt\fR argument and the numeric value being converted
326 320 as the first and only \fIexpr\fR argument. Any other numeric value is
327 321 converted to a string by the equivalent of a call to the \fBsprintf\fR function
328 322 with the value of the variable \fBCONVFMT\fR as the \fBfmt\fR argument and the
329 323 numeric value being converted as the first and only \fIexpr\fR argument.
330 324 .sp
331 325 .LP
332 326 A string value is considered to be a \fInumeric string\fR in the following
333 327 case:
334 328 .RS +4
335 329 .TP
336 330 1.
337 331 Any leading and trailing blank characters is ignored.
338 332 .RE
339 333 .RS +4
340 334 .TP
341 335 2.
342 336 If the first unignored character is a \fB+\fR or \fB\(mi\fR, it is ignored.
343 337 .RE
344 338 .RS +4
345 339 .TP
346 340 3.
347 341 If the remaining unignored characters would be lexically recognized as a
348 342 \fBNUMBER\fR token, the string is considered a \fInumeric string\fR.
349 343 .RE
350 344 .sp
351 345 .LP
352 346 If a \fB\(mi\fR character is ignored in the above steps, the numeric value of
353 347 the \fInumeric string\fR is the negation of the numeric value of the recognized
354 348 \fBNUMBER\fR token. Otherwise the numeric value of the \fInumeric string\fR is
355 349 the numeric value of the recognized \fBNUMBER\fR token. Whether or not a string
356 350 is a \fInumeric string\fR is relevant only in contexts where that term is used
357 351 in this section.
358 352 .sp
359 353 .LP
360 354 When an expression is used in a Boolean context, if it has a numeric value, a
361 355 value of zero is treated as false and any other value is treated as true.
362 356 Otherwise, a string value of the null string is treated as false and any other
363 357 value is treated as true. A Boolean context is one of the following:
364 358 .RS +4
365 359 .TP
366 360 .ie t \(bu
367 361 .el o
368 362 the first subexpression of a conditional expression.
369 363 .RE
370 364 .RS +4
371 365 .TP
372 366 .ie t \(bu
373 367 .el o
374 368 an expression operated on by logical NOT, logical \fBAND,\fR or logical OR.
375 369 .RE
376 370 .RS +4
377 371 .TP
378 372 .ie t \(bu
379 373 .el o
380 374 the second expression of a \fBfor\fR statement.
381 375 .RE
382 376 .RS +4
383 377 .TP
384 378 .ie t \(bu
385 379 .el o
386 380 the expression of an \fBif\fR statement.
387 381 .RE
388 382 .RS +4
389 383 .TP
390 384 .ie t \(bu
391 385 .el o
392 386 the expression of the \fBwhile\fR clause in either a \fBwhile\fR or \fBdo\fR
393 387 \fB\&.\|.\|.\fR \fBwhile\fR statement.
394 388 .RE
395 389 .RS +4
396 390 .TP
397 391 .ie t \(bu
398 392 .el o
399 393 an expression used as a pattern (as in Overall Program Structure).
400 394 .RE
401 395 .sp
402 396 .LP
403 397 The \fBnawk\fR language supplies arrays that are used for storing numbers or
404 398 strings. Arrays need not be declared. They are initially empty, and their sizes
405 399 changes dynamically. The subscripts, or element identifiers, are strings,
406 400 providing a type of associative array capability. An array name followed by a
407 401 subscript within square brackets can be used as an \fIlvalue\fR and as an
408 402 expression, as described in the grammar. Unsubscripted array names are used in
409 403 only the following contexts:
410 404 .RS +4
411 405 .TP
412 406 .ie t \(bu
413 407 .el o
414 408 a parameter in a function definition or function call.
415 409 .RE
416 410 .RS +4
417 411 .TP
418 412 .ie t \(bu
419 413 .el o
420 414 the \fBNAME\fR token following any use of the keyword \fBin\fR.
421 415 .RE
422 416 .sp
423 417 .LP
424 418 A valid array \fIindex\fR consists of one or more comma-separated expressions,
425 419 similar to the way in which multi-dimensional arrays are indexed in some
426 420 programming languages. Because \fBnawk\fR arrays are really one-dimensional,
427 421 such a comma-separated list is converted to a single string by concatenating
428 422 the string values of the separate expressions, each separated from the other by
429 423 the value of the \fBSUBSEP\fR variable.
430 424 .sp
431 425 .LP
432 426 Thus, the following two index operations are equivalent:
433 427 .sp
434 428 .in +2
435 429 .nf
436 430 var[expr1, expr2, ... exprn]
437 431 var[expr1 SUBSEP expr2 SUBSEP ... SUBSEP exprn]
438 432 .fi
439 433 .in -2
440 434
441 435 .sp
442 436 .LP
443 437 A multi-dimensioned \fIindex\fR used with the \fBin\fR operator must be put in
444 438 parentheses. The \fBin\fR operator, which tests for the existence of a
445 439 particular array element, does not create the element if it does not exist.
446 440 Any other reference to a non-existent array element automatically creates it.
447 441 .SS "Variables and Special Variables"
448 442 .sp
449 443 .LP
450 444 Variables can be used in an \fBnawk\fR program by referencing them. With the
451 445 exception of function parameters, they are not explicitly declared.
452 446 Uninitialized scalar variables and array elements have both a numeric value of
453 447 zero and a string value of the empty string.
454 448 .sp
455 449 .LP
456 450 Field variables are designated by a \fB$\fR followed by a number or numerical
457 451 expression. The effect of the field number \fIexpression\fR evaluating to
458 452 anything other than a non-negative integer is unspecified. Uninitialized
459 453 variables or string values need not be converted to numeric values in this
460 454 context. New field variables are created by assigning a value to them.
461 455 References to non-existent fields (that is, fields after \fB$NF\fR) produce the
462 456 null string. However, assigning to a non-existent field (for example,
463 457 \fB$(NF+2) = 5\fR) increases the value of \fBNF\fR, create any intervening
464 458 fields with the null string as their values and cause the value of \fB$0\fR to
465 459 be recomputed, with the fields being separated by the value of \fBOFS\fR. Each
466 460 field variable has a string value when created. If the string, with any
467 461 occurrence of the decimal-point character from the current locale changed to a
468 462 period character, is considered a \fInumeric string\fR (see \fBExpressions in
469 463 nawk\fR above), the field variable also has the numeric value of the \fInumeric
470 464 string\fR.
471 465 .SS "/usr/bin/nawk, /usr/xpg4/bin/awk"
472 466 .sp
473 467 .LP
474 468 \fBnawk\fR sets the following special variables that are supported by both
475 469 \fB/usr/bin/nawk\fR and \fB/usr/xpg4/bin/awk\fR:
476 470 .sp
477 471 .ne 2
478 472 .na
479 473 \fB\fBARGC\fR\fR
480 474 .ad
481 475 .RS 12n
482 476 The number of elements in the \fBARGV\fR array.
483 477 .RE
484 478
485 479 .sp
486 480 .ne 2
487 481 .na
488 482 \fB\fBARGV\fR\fR
489 483 .ad
490 484 .RS 12n
491 485 An array of command line arguments, excluding options and the \fIprogram\fR
492 486 argument, numbered from zero to \fBARGC\fR\(mi1.
493 487 .sp
494 488 The arguments in \fBARGV\fR can be modified or added to; \fBARGC\fR can be
495 489 altered. As each input file ends, \fBnawk\fR treats the next non-null element
496 490 of \fBARGV\fR, up to the current value of \fBARGC\fR\(mi1, inclusive, as the
497 491 name of the next input file. Setting an element of \fBARGV\fR to null means
498 492 that it is not treated as an input file. The name \fB\(mi\fR indicates the
499 493 standard input. If an argument matches the format of an \fIassignment\fR
500 494 operand, this argument is treated as an assignment rather than a \fIfile\fR
501 495 argument.
502 496 .RE
503 497
504 498 .sp
505 499 .ne 2
506 500 .na
507 501 \fB\fBENVIRON\fR\fR
508 502 .ad
509 503 .RS 12n
510 504 The variable \fBENVIRON\fR is an array representing the value of the
511 505 environment. The indices of the array are strings consisting of the names of
512 506 the environment variables, and the value of each array element is a string
513 507 consisting of the value of that variable. If the value of an environment
514 508 variable is considered a \fInumeric string\fR, the array element also has its
515 509 numeric value.
516 510 .sp
517 511 In all cases where \fBnawk\fR behavior is affected by environment variables
518 512 (including the environment of any commands that \fBnawk\fR executes via the
519 513 \fBsystem\fR function or via pipeline redirections with the \fBprint\fR
520 514 statement, the \fBprintf\fR statement, or the \fBgetline\fR function), the
521 515 environment used is the environment at the time \fBnawk\fR began executing.
522 516 .RE
523 517
524 518 .sp
525 519 .ne 2
526 520 .na
527 521 \fB\fBFILENAME\fR\fR
528 522 .ad
529 523 .RS 12n
530 524 A pathname of the current input file. Inside a \fBBEGIN\fR action the value is
531 525 undefined. Inside an \fBEND\fR action the value is the name of the last input
532 526 file processed.
533 527 .RE
534 528
535 529 .sp
536 530 .ne 2
537 531 .na
538 532 \fB\fBFNR\fR\fR
539 533 .ad
540 534 .RS 12n
541 535 The ordinal number of the current record in the current file. Inside a
542 536 \fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
543 537 the number of the last record processed in the last file processed.
544 538 .RE
545 539
546 540 .sp
547 541 .ne 2
548 542 .na
549 543 \fB\fBFS\fR\fR
550 544 .ad
551 545 .RS 12n
552 546 Input field separator regular expression; a space character by default.
553 547 .RE
554 548
555 549 .sp
556 550 .ne 2
557 551 .na
558 552 \fB\fBNF\fR\fR
559 553 .ad
560 554 .RS 12n
561 555 The number of fields in the current record. Inside a \fBBEGIN\fR action, the
562 556 use of \fBNF\fR is undefined unless a \fBgetline\fR function without a
563 557 \fIvar\fR argument is executed previously. Inside an \fBEND\fR action, \fBNF\fR
564 558 retains the value it had for the last record read, unless a subsequent,
565 559 redirected, \fBgetline\fR function without a \fIvar\fR argument is performed
566 560 prior to entering the \fBEND\fR action.
567 561 .RE
568 562
569 563 .sp
570 564 .ne 2
571 565 .na
572 566 \fB\fBNR\fR\fR
573 567 .ad
574 568 .RS 12n
575 569 The ordinal number of the current record from the start of input. Inside a
576 570 \fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
577 571 the number of the last record processed.
578 572 .RE
579 573
580 574 .sp
581 575 .ne 2
582 576 .na
583 577 \fB\fBOFMT\fR\fR
584 578 .ad
585 579 .RS 12n
586 580 The \fBprintf\fR format for converting numbers to strings in output statements
587 581 \fB"%.6g"\fR by default. The result of the conversion is unspecified if the
588 582 value of \fBOFMT\fR is not a floating-point format specification.
589 583 .RE
590 584
591 585 .sp
592 586 .ne 2
593 587 .na
594 588 \fB\fBOFS\fR\fR
595 589 .ad
596 590 .RS 12n
597 591 The \fBprint\fR statement output field separator; a space character by default.
598 592 .RE
599 593
600 594 .sp
601 595 .ne 2
602 596 .na
603 597 \fB\fBORS\fR\fR
604 598 .ad
605 599 .RS 12n
606 600 The \fBprint\fR output record separator; a newline character by default.
607 601 .RE
608 602
609 603 .sp
610 604 .ne 2
611 605 .na
612 606 \fB\fBLENGTH\fR\fR
613 607 .ad
614 608 .RS 12n
615 609 The length of the string matched by the \fBmatch\fR function.
616 610 .RE
617 611
618 612 .sp
619 613 .ne 2
620 614 .na
621 615 \fB\fBRS\fR\fR
622 616 .ad
623 617 .RS 12n
624 618 The first character of the string value of \fBRS\fR is the input record
625 619 separator; a newline character by default. If \fBRS\fR contains more than one
626 620 character, the results are unspecified. If \fBRS\fR is null, then records are
627 621 separated by sequences of one or more blank lines. Leading or trailing blank
628 622 lines do not produce empty records at the beginning or end of input, and the
629 623 field separator is always newline, no matter what the value of \fBFS\fR.
630 624 .RE
631 625
632 626 .sp
633 627 .ne 2
634 628 .na
635 629 \fB\fBRSTART\fR\fR
636 630 .ad
637 631 .RS 12n
638 632 The starting position of the string matched by the \fBmatch\fR function,
639 633 numbering from 1. This is always equivalent to the return value of the
640 634 \fBmatch\fR function.
641 635 .RE
642 636
643 637 .sp
644 638 .ne 2
645 639 .na
646 640 \fB\fBSUBSEP\fR\fR
647 641 .ad
648 642 .RS 12n
649 643 The subscript separator string for multi-dimensional arrays. The default value
650 644 is \fB\e034\fR\&.
651 645 .RE
652 646
653 647 .SS "/usr/xpg4/bin/awk"
654 648 .sp
655 649 .LP
656 650 The following variable is supported for \fB/usr/xpg4/bin/awk\fR only:
657 651 .sp
658 652 .ne 2
659 653 .na
660 654 \fB\fBCONVFMT\fR\fR
661 655 .ad
662 656 .RS 11n
663 657 The \fBprintf\fR format for converting numbers to strings (except for output
664 658 statements, where \fBOFMT\fR is used). The default is \fB%.6g\fR.
665 659 .RE
666 660
667 661 .SS "Regular Expressions"
668 662 .sp
669 663 .LP
670 664 The \fBnawk\fR utility makes use of the extended regular expression notation
671 665 (see \fBregex\fR(5)) except that it allows the use of C-language conventions to
672 666 escape special characters within the EREs, namely \fB\e\e\fR, \fB\ea\fR,
673 667 \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR, and those
674 668 specified in the following table. These escape sequences are recognized both
675 669 inside and outside bracket expressions. Note that records need not be
676 670 separated by newline characters and string constants can contain newline
677 671 characters, so even the \fB\en\fR sequence is valid in \fBnawk\fR EREs. Using
678 672 a slash character within the regular expression requires escaping as shown in
679 673 the table below:
680 674 .sp
681 675
682 676 .sp
683 677 .TS
684 678 l l l
685 679 l l l .
686 680 \fBEscape Sequence\fR \fBDescription\fR \fBMeaning\fR
687 681 _
688 682 \fB\e"\fR Backslash quotation-mark Quotation-mark character
689 683 _
690 684 \fB\e/\fR Backslash slash Slash character
691 685 _
692 686 \fB\e\fR\fIddd\fR T{
693 687 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.
694 688 T} T{
695 689 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.
696 690 T}
697 691 _
698 692 \fB\e\fR\fIc\fR T{
699 693 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).
700 694 T} Undefined
701 695 .TE
702 696
703 697 .sp
704 698 .LP
705 699 A regular expression can be matched against a specific field or string by using
706 700 one of the two regular expression matching operators, \fB~\fR and \fB!\|~\fR.
707 701 These operators interpret their right-hand operand as a regular expression and
708 702 their left-hand operand as a string. If the regular expression matches the
709 703 string, the \fB~\fR expression evaluates to the value \fB1\fR, and the
710 704 \fB!\|~\fR expression evaluates to the value \fB0\fR. If the regular expression
711 705 does not match the string, the \fB~\fR expression evaluates to the value
712 706 \fB0\fR, and the \fB!\|~\fR expression evaluates to the value \fB1\fR. If the
713 707 right-hand operand is any expression other than the lexical token \fBERE\fR,
714 708 the string value of the expression is interpreted as an extended regular
715 709 expression, including the escape conventions described above. Notice that these
716 710 same escape conventions also are applied in the determining the value of a
717 711 string literal (the lexical token \fBSTRING\fR), and is applied a second time
718 712 when a string literal is used in this context.
719 713 .sp
720 714 .LP
721 715 When an \fBERE\fR token appears as an expression in any context other than as
722 716 the right-hand of the \fB~\fR or \fB!\|~\fR operator or as one of the built-in
723 717 function arguments described below, the value of the resulting expression is
724 718 the equivalent of:
725 719 .sp
726 720 .in +2
727 721 .nf
728 722 $0 ~ /\fIere\fR/
729 723 .fi
730 724 .in -2
731 725
732 726 .sp
733 727 .LP
734 728 The \fIere\fR argument to the \fBgsub,\fR \fBmatch,\fR \fBsub\fR functions, and
735 729 the \fIfs\fR argument to the \fBsplit\fR function (see \fBString Functions\fR)
736 730 is interpreted as extended regular expressions. These can be either \fBERE\fR
737 731 tokens or arbitrary expressions, and are interpreted in the same manner as the
738 732 right-hand side of the \fB~\fR or \fB!\|~\fR operator.
739 733 .sp
740 734 .LP
741 735 An extended regular expression can be used to separate fields by using the
742 736 \fB-F\fR \fIERE\fR option or by assigning a string containing the expression to
743 737 the built-in variable \fBFS\fR. The default value of the \fBFS\fR variable is a
744 738 single space character. The following describes \fBFS\fR behavior:
745 739 .RS +4
746 740 .TP
747 741 1.
748 742 If \fBFS\fR is a single character:
749 743 .RS +4
750 744 .TP
751 745 .ie t \(bu
752 746 .el o
753 747 If \fBFS\fR is the space character, skip leading and trailing blank characters;
754 748 fields are delimited by sets of one or more blank characters.
755 749 .RE
756 750 .RS +4
757 751 .TP
758 752 .ie t \(bu
759 753 .el o
760 754 Otherwise, if \fBFS\fR is any other character \fIc\fR, fields are delimited by
761 755 each single occurrence of \fIc\fR.
762 756 .RE
763 757 .RE
764 758 .RS +4
765 759 .TP
766 760 2.
767 761 Otherwise, the string value of \fBFS\fR is considered to be an extended
768 762 regular expression. Each occurrence of a sequence matching the extended regular
769 763 expression delimits fields.
770 764 .RE
771 765 .sp
772 766 .LP
773 767 Except in the \fBgsub\fR, \fBmatch\fR, \fBsplit\fR, and \fBsub\fR built-in
774 768 functions, regular expression matching is based on input records. That is,
775 769 record separator characters (the first character of the value of the variable
776 770 \fBRS\fR, a newline character by default) cannot be embedded in the expression,
777 771 and no expression matches the record separator character. If the record
778 772 separator is not a newline character, newline characters embedded in the
779 773 expression can be matched. In those four built-in functions, regular expression
780 774 matching are based on text strings. So, any character (including the newline
781 775 character and the record separator) can be embedded in the pattern and an
782 776 appropriate pattern matches any character. However, in all \fBnawk\fR regular
783 777 expression matching, the use of one or more NULL characters in the pattern,
784 778 input record or text string produces undefined results.
785 779 .SS "Patterns"
786 780 .sp
787 781 .LP
788 782 A \fIpattern\fR is any valid \fIexpression,\fR a range specified by two
789 783 expressions separated by comma, or one of the two special patterns \fBBEGIN\fR
790 784 or \fBEND\fR.
791 785 .SS "Special Patterns"
792 786 .sp
793 787 .LP
794 788 The \fBnawk\fR utility recognizes two special patterns, \fBBEGIN\fR and
795 789 \fBEND\fR. Each \fBBEGIN\fR pattern is matched once and its associated action
796 790 executed before the first record of input is read (except possibly by use of
797 791 the \fBgetline\fR function in a prior \fBBEGIN\fR action) and before command
798 792 line assignment is done. Each \fBEND\fR pattern is matched once and its
799 793 associated action executed after the last record of input has been read. These
800 794 two patterns have associated actions.
801 795 .sp
802 796 .LP
803 797 \fBBEGIN\fR and \fBEND\fR do not combine with other patterns. Multiple
804 798 \fBBEGIN\fR and \fBEND\fR patterns are allowed. The actions associated with the
805 799 \fBBEGIN\fR patterns are executed in the order specified in the program, as are
806 800 the \fBEND\fR actions. An \fBEND\fR pattern can precede a \fBBEGIN\fR pattern
807 801 in a program.
808 802 .sp
809 803 .LP
810 804 If an \fBnawk\fR program consists of only actions with the pattern \fBBEGIN\fR,
811 805 and the \fBBEGIN\fR action contains no \fBgetline\fR function, \fBnawk\fR exits
812 806 without reading its input when the last statement in the last \fBBEGIN\fR
813 807 action is executed. If an \fBnawk\fR program consists of only actions with the
814 808 pattern \fBEND\fR or only actions with the patterns \fBBEGIN\fR and \fBEND\fR,
815 809 the input is read before the statements in the \fBEND\fR actions are executed.
816 810 .SS "Expression Patterns"
817 811 .sp
818 812 .LP
819 813 An expression pattern is evaluated as if it were an expression in a Boolean
820 814 context. If the result is true, the pattern is considered to match, and the
821 815 associated action (if any) is executed. If the result is false, the action is
822 816 not executed.
823 817 .SS "Pattern Ranges"
824 818 .sp
825 819 .LP
826 820 A pattern range consists of two expressions separated by a comma. In this case,
827 821 the action is performed for all records between a match of the first expression
828 822 and the following match of the second expression, inclusive. At this point, the
829 823 pattern range can be repeated starting at input records subsequent to the end
830 824 of the matched range.
831 825 .SS "Actions"
832 826 .sp
833 827 .LP
834 828 An action is a sequence of statements. A statement can be one of the following:
835 829 .sp
836 830 .in +2
837 831 .nf
838 832 if ( \fIexpression\fR ) \fIstatement\fR [ else \fIstatement\fR ]
839 833 while ( \fIexpression\fR ) \fIstatement\fR
840 834 do \fIstatement\fR while ( \fIexpression\fR )
841 835 for ( \fIexpression\fR ; \fIexpression\fR ; \fIexpression\fR ) \fIstatement\fR
842 836 for ( \fIvar\fR in \fIarray\fR ) \fIstatement\fR
843 837 delete \fIarray\fR[\fIsubscript\fR] #delete an array element
844 838 break
845 839 continue
846 840 { [ \fIstatement\fR ] .\|.\|. }
847 841 \fIexpression\fR # commonly variable = expression
848 842 print [ \fIexpression-list\fR ] [ >\fIexpression\fR ]
849 843 printf format [ ,\fIexpression-list\fR ] [ >\fIexpression\fR ]
850 844 next # skip remaining patterns on this input line
851 845 exit [expr] # skip the rest of the input; exit status is expr
852 846 return [expr]
853 847 .fi
854 848 .in -2
855 849
856 850 .sp
857 851 .LP
858 852 Any single statement can be replaced by a statement list enclosed in braces.
859 853 The statements are terminated by newline characters or semicolons, and are
860 854 executed sequentially in the order that they appear.
861 855 .sp
862 856 .LP
863 857 The \fBnext\fR statement causes all further processing of the current input
864 858 record to be abandoned. The behavior is undefined if a \fBnext\fR statement
865 859 appears or is invoked in a \fBBEGIN\fR or \fBEND\fR action.
866 860 .sp
867 861 .LP
868 862 The \fBexit\fR statement invokes all \fBEND\fR actions in the order in which
869 863 they occur in the program source and then terminate the program without reading
870 864 further input. An \fBexit\fR statement inside an \fBEND\fR action terminates
871 865 the program without further execution of \fBEND\fR actions. If an expression
872 866 is specified in an \fBexit\fR statement, its numeric value is the exit status
873 867 of \fBnawk\fR, unless subsequent errors are encountered or a subsequent
874 868 \fBexit\fR statement with an expression is executed.
875 869 .SS "Output Statements"
876 870 .sp
877 871 .LP
878 872 Both \fBprint\fR and \fBprintf\fR statements write to standard output by
879 873 default. The output is written to the location specified by
880 874 \fIoutput_redirection\fR if one is supplied, as follows:
881 875 .sp
882 876 .in +2
883 877 .nf
884 878 \fB>\fR \fIexpression\fR\fB>>\fR \fIexpression\fR\fB|\fR \fIexpression\fR
885 879 .fi
886 880 .in -2
887 881
888 882 .sp
889 883 .LP
890 884 In all cases, the \fIexpression\fR is evaluated to produce a string that is
891 885 used as a full pathname to write into (for \fB>\fR or \fB>>\fR) or as a command
892 886 to be executed (for \fB|\fR). Using the first two forms, if the file of that
893 887 name is not currently open, it is opened, creating it if necessary and using
894 888 the first form, truncating the file. The output then is appended to the file.
895 889 As long as the file remains open, subsequent calls in which \fIexpression\fR
896 890 evaluates to the same string value simply appends output to the file. The file
897 891 remains open until the \fBclose\fR function, which is called with an expression
898 892 that evaluates to the same string value.
899 893 .sp
900 894 .LP
901 895 The third form writes output onto a stream piped to the input of a command. The
902 896 stream is created if no stream is currently open with the value of
903 897 \fIexpression\fR as its command name. The stream created is equivalent to one
904 898 created by a call to the \fBpopen\fR(3C) function with the value of
905 899 \fIexpression\fR as the \fIcommand\fR argument and a value of \fBw\fR as the
906 900 \fImode\fR argument. As long as the stream remains open, subsequent calls in
907 901 which \fIexpression\fR evaluates to the same string value writes output to the
908 902 existing stream. The stream remains open until the \fBclose\fR function is
909 903 called with an expression that evaluates to the same string value. At that
910 904 time, the stream is closed as if by a call to the \fBpclose\fR function.
911 905 .sp
912 906 .LP
913 907 These output statements take a comma-separated list of \fIexpression\fR \fIs\fR
914 908 referred in the grammar by the non-terminal symbols \fBexpr_list,\fR
915 909 \fBprint_expr_list\fR or \fBprint_expr_list_opt.\fR This list is referred to
916 910 here as the \fIexpression list\fR, and each member is referred to as an
917 911 \fIexpression argument\fR.
918 912 .sp
919 913 .LP
920 914 The \fBprint\fR statement writes the value of each expression argument onto the
921 915 indicated output stream separated by the current output field separator (see
922 916 variable \fBOFS\fR above), and terminated by the output record separator (see
923 917 variable \fBORS\fR above). All expression arguments is taken as strings, being
924 918 converted if necessary; with the exception that the \fBprintf\fR format in
925 919 \fBOFMT\fR is used instead of the value in \fBCONVFMT\fR. An empty expression
926 920 list stands for the whole input record \fB(\fR$0\fB)\fR.
927 921 .sp
928 922 .LP
929 923 The \fBprintf\fR statement produces output based on a notation similar to the
930 924 File Format Notation used to describe file formats in this document Output is
931 925 produced as specified with the first expression argument as the string
932 926 \fBformat\fR and subsequent expression arguments as the strings \fBarg1\fR to
933 927 \fBargn,\fR inclusive, with the following exceptions:
934 928 .RS +4
935 929 .TP
936 930 1.
937 931 The \fIformat\fR is an actual character string rather than a graphical
938 932 representation. Therefore, it cannot contain empty character positions. The
939 933 space character in the \fIformat\fR string, in any context other than a
940 934 \fIflag\fR of a conversion specification, is treated as an ordinary character
941 935 that is copied to the output.
942 936 .RE
943 937 .RS +4
944 938 .TP
945 939 2.
946 940 If the character set contains a Delta character and that character appears
947 941 in the \fIformat\fR string, it is treated as an ordinary character that is
948 942 copied to the output.
949 943 .RE
950 944 .RS +4
951 945 .TP
952 946 3.
953 947 The \fIescape sequences\fR beginning with a backslash character is treated
954 948 as sequences of ordinary characters that are copied to the output. Note that
955 949 these same sequences is interpreted lexically by \fBnawk\fR when they appear in
956 950 literal strings, but they is not treated specially by the \fBprintf\fR
957 951 statement.
958 952 .RE
959 953 .RS +4
960 954 .TP
961 955 4.
962 956 A \fIfield width\fR or \fIprecision\fR can be specified as the \fB*\fR
963 957 character instead of a digit string. In this case the next argument from the
964 958 expression list is fetched and its numeric value taken as the field width or
965 959 precision.
966 960 .RE
967 961 .RS +4
968 962 .TP
969 963 5.
970 964 The implementation does not precede or follow output from the \fBd\fR or
971 965 \fBu\fR conversion specifications with blank characters not specified by the
972 966 \fIformat\fR string.
973 967 .RE
974 968 .RS +4
975 969 .TP
976 970 6.
977 971 The implementation does not precede output from the \fBo\fR conversion
978 972 specification with leading zeros not specified by the \fIformat\fR string.
979 973 .RE
980 974 .RS +4
981 975 .TP
982 976 7.
983 977 For the \fBc\fR conversion specification: if the argument has a numeric
984 978 value, the character whose encoding is that value is output. If the value is
985 979 zero or is not the encoding of any character in the character set, the behavior
986 980 is undefined. If the argument does not have a numeric value, the first
987 981 character of the string value is output; if the string does not contain any
988 982 characters the behavior is undefined.
989 983 .RE
990 984 .RS +4
991 985 .TP
992 986 8.
993 987 For each conversion specification that consumes an argument, the next
994 988 expression argument is evaluated. With the exception of the \fBc\fR conversion,
995 989 the value is converted to the appropriate type for the conversion
996 990 specification.
997 991 .RE
998 992 .RS +4
999 993 .TP
1000 994 9.
1001 995 If there are insufficient expression arguments to satisfy all the conversion
1002 996 specifications in the \fIformat\fR string, the behavior is undefined.
1003 997 .RE
1004 998 .RS +4
1005 999 .TP
1006 1000 10.
1007 1001 If any character sequence in the \fIformat\fR string begins with a %
1008 1002 character, but does not form a valid conversion specification, the behavior is
1009 1003 unspecified.
1010 1004 .RE
1011 1005 .sp
1012 1006 .LP
1013 1007 Both \fBprint\fR and \fBprintf\fR can output at least \fB{LINE_MAX}\fR bytes.
1014 1008 .SS "Functions"
1015 1009 .sp
1016 1010 .LP
1017 1011 The \fBnawk\fR language has a variety of built-in functions: arithmetic,
1018 1012 string, input/output and general.
1019 1013 .SS "Arithmetic Functions"
1020 1014 .sp
1021 1015 .LP
1022 1016 The arithmetic functions, except for \fBint\fR, are based on the \fBISO\fR
1023 1017 \fBC\fR standard. The behavior is undefined in cases where the \fBISO\fR
1024 1018 \fBC\fR standard specifies that an error be returned or that the behavior is
1025 1019 undefined. Although the grammar permits built-in functions to appear with no
1026 1020 arguments or parentheses, unless the argument or parentheses are indicated as
1027 1021 optional in the following list (by displaying them within the \fB[ ]\fR
1028 1022 brackets), such use is undefined.
1029 1023 .sp
1030 1024 .ne 2
1031 1025 .na
1032 1026 \fB\fBatan2(\fR\fIy\fR,\fIx\fR\fB)\fR\fR
1033 1027 .ad
1034 1028 .RS 17n
1035 1029 Return arctangent of \fIy\fR/\fIx\fR.
1036 1030 .RE
1037 1031
1038 1032 .sp
1039 1033 .ne 2
1040 1034 .na
1041 1035 \fB\fBcos\fR(\fIx\fR)\fR
1042 1036 .ad
1043 1037 .RS 17n
1044 1038 Return cosine of \fIx,\fR where \fIx\fR is in radians.
1045 1039 .RE
1046 1040
1047 1041 .sp
1048 1042 .ne 2
1049 1043 .na
1050 1044 \fB\fBsin\fR(\fIx\fR)\fR
1051 1045 .ad
1052 1046 .RS 17n
1053 1047 Return sine of \fIx,\fR where \fIx\fR is in radians.
1054 1048 .RE
1055 1049
1056 1050 .sp
1057 1051 .ne 2
1058 1052 .na
1059 1053 \fB\fBexp\fR(\fIx\fR)\fR
1060 1054 .ad
1061 1055 .RS 17n
1062 1056 Return the exponential function of \fIx\fR.
1063 1057 .RE
1064 1058
1065 1059 .sp
1066 1060 .ne 2
1067 1061 .na
1068 1062 \fB\fBlog\fR(\fIx\fR)\fR
1069 1063 .ad
1070 1064 .RS 17n
1071 1065 Return the natural logarithm of \fIx\fR.
1072 1066 .RE
1073 1067
1074 1068 .sp
1075 1069 .ne 2
1076 1070 .na
1077 1071 \fB\fBsqrt\fR(\fIx\fR)\fR
1078 1072 .ad
1079 1073 .RS 17n
1080 1074 Return the square root of \fIx\fR.
1081 1075 .RE
1082 1076
1083 1077 .sp
1084 1078 .ne 2
1085 1079 .na
1086 1080 \fB\fBint\fR(\fIx\fR)\fR
1087 1081 .ad
1088 1082 .RS 17n
1089 1083 Truncate its argument to an integer. It is truncated toward 0 when \fIx\fR > 0.
1090 1084 .RE
1091 1085
1092 1086 .sp
1093 1087 .ne 2
1094 1088 .na
1095 1089 \fB\fBrand()\fR\fR
1096 1090 .ad
1097 1091 .RS 17n
1098 1092 Return a random number \fIn\fR, such that 0 \(<= \fIn\fR < 1.
1099 1093 .RE
1100 1094
1101 1095 .sp
1102 1096 .ne 2
1103 1097 .na
1104 1098 \fB\fBsrand\fR([\fBexpr\fR])\fR
1105 1099 .ad
1106 1100 .RS 17n
1107 1101 Set the seed value for \fBrand\fR to \fIexpr\fR or use the time of day if
1108 1102 \fIexpr\fR is omitted. The previous seed value is returned.
1109 1103 .RE
1110 1104
1111 1105 .SS "String Functions"
1112 1106 .sp
1113 1107 .LP
1114 1108 The string functions in the following list shall be supported. Although the
1115 1109 grammar permits built-in functions to appear with no arguments or parentheses,
1116 1110 unless the argument or parentheses are indicated as optional in the following
1117 1111 list (by displaying them within the \fB[ ]\fR brackets), such use is undefined.
1118 1112 .sp
1119 1113 .ne 2
1120 1114 .na
1121 1115 \fB\fBgsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
1122 1116 .ad
1123 1117 .sp .6
1124 1118 .RS 4n
1125 1119 Behave like \fBsub\fR (see below), except that it replaces all occurrences of
1126 1120 the regular expression (like the \fBed\fR utility global substitute) in
1127 1121 \fB$0\fR or in the \fIin\fR argument, when specified.
1128 1122 .RE
1129 1123
1130 1124 .sp
1131 1125 .ne 2
1132 1126 .na
1133 1127 \fB\fBindex\fR(\fIs\fR,\fIt\fR)\fR
1134 1128 .ad
1135 1129 .sp .6
1136 1130 .RS 4n
1137 1131 Return the position, in characters, numbering from 1, in string \fIs\fR where
1138 1132 string \fIt\fR first occurs, or zero if it does not occur at all.
1139 1133 .RE
1140 1134
1141 1135 .sp
1142 1136 .ne 2
1143 1137 .na
1144 1138 \fB\fBlength\fR[([\fIs\fR])]\fR
1145 1139 .ad
1146 1140 .sp .6
1147 1141 .RS 4n
1148 1142 Return the length, in characters, of its argument taken as a string, or of the
1149 1143 whole record, \fB$0\fR, if there is no argument.
1150 1144 .RE
1151 1145
1152 1146 .sp
1153 1147 .ne 2
1154 1148 .na
1155 1149 \fB\fBmatch\fR(\fIs\fR,\fIere\fR)\fR
1156 1150 .ad
1157 1151 .sp .6
1158 1152 .RS 4n
1159 1153 Return the position, in characters, numbering from 1, in string \fIs\fR where
1160 1154 the extended regular expression \fIere\fR occurs, or zero if it does not occur
1161 1155 at all. \fBRSTART\fR is set to the starting position (which is the same as the
1162 1156 returned value), zero if no match is found; \fBRLENGTH\fR is set to the length
1163 1157 of the matched string, \(mi1 if no match is found.
1164 1158 .RE
1165 1159
1166 1160 .sp
1167 1161 .ne 2
1168 1162 .na
1169 1163 \fB\fBsplit\fR(\fIs\fR,\fIa\fR[,\|\fIfs\fR])\fR
1170 1164 .ad
1171 1165 .sp .6
1172 1166 .RS 4n
1173 1167 Split the string \fIs\fR into array elements \fIa\fR[1], \fIa\fR[2],
1174 1168 \fB\&...,\fR \fIa\fR[\fIn\fR], and return \fIn\fR. The separation is done with
1175 1169 the extended regular expression \fIfs\fR or with the field separator \fBFS\fR
1176 1170 if \fIfs\fR is not given. Each array element has a string value when created.
1177 1171 If the string assigned to any array element, with any occurrence of the
1178 1172 decimal-point character from the current locale changed to a period character,
1179 1173 would be considered a \fInumeric string\fR; the array element also has the
1180 1174 numeric value of the \fInumeric string\fR. The effect of a null string as the
1181 1175 value of \fIfs\fR is unspecified.
1182 1176 .RE
1183 1177
1184 1178 .sp
1185 1179 .ne 2
1186 1180 .na
1187 1181 \fB\fBsprintf\fR(\fBfmt\fR,\fIexpr\fR,\fIexpr\fR,\fB\&...\fR)\fR
1188 1182 .ad
1189 1183 .sp .6
1190 1184 .RS 4n
1191 1185 Format the expressions according to the \fBprintf\fR format given by \fIfmt\fR
1192 1186 and return the resulting string.
1193 1187 .RE
1194 1188
1195 1189 .sp
1196 1190 .ne 2
1197 1191 .na
1198 1192 \fB\fBsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
1199 1193 .ad
1200 1194 .sp .6
1201 1195 .RS 4n
1202 1196 Substitute the string \fIrepl\fR in place of the first instance of the extended
1203 1197 regular expression \fBERE\fR in string in and return the number of
1204 1198 substitutions. An ampersand ( \fB&\fR ) appearing in the string \fIrepl\fR is
1205 1199 replaced by the string from in that matches the regular expression. An
1206 1200 ampersand preceded with a backslash ( \fB\e\fR ) is interpreted as the literal
1207 1201 ampersand character. An occurrence of two consecutive backslashes is
1208 1202 interpreted as just a single literal backslash character. Any other occurrence
1209 1203 of a backslash (for example, preceding any other character) is treated as a
1210 1204 literal backslash character. If \fIrepl\fR is a string literal, the handling of
1211 1205 the ampersand character occurs after any lexical processing, including any
1212 1206 lexical backslash escape sequence processing. If \fBin\fR is specified and it
1213 1207 is not an \fBlvalue\fR the behavior is undefined. If in is omitted, \fBnawk\fR
1214 1208 uses the current record (\fB$0\fR) in its place.
1215 1209 .RE
1216 1210
1217 1211 .sp
1218 1212 .ne 2
1219 1213 .na
1220 1214 \fB\fBsubstr\fR(\fIs\fR,\fIm\fR[,\|\fIn\fR])\fR
1221 1215 .ad
1222 1216 .sp .6
1223 1217 .RS 4n
1224 1218 Return the at most \fIn\fR-character substring of \fIs\fR that begins at
1225 1219 position \fIm,\fR numbering from 1. If \fIn\fR is missing, the length of the
1226 1220 substring is limited by the length of the string \fIs\fR.
1227 1221 .RE
1228 1222
1229 1223 .sp
1230 1224 .ne 2
1231 1225 .na
1232 1226 \fB\fBtolower\fR(\fIs\fR)\fR
1233 1227 .ad
1234 1228 .sp .6
1235 1229 .RS 4n
1236 1230 Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
1237 1231 an upper-case letter specified to have a \fBtolower\fR mapping by the
1238 1232 \fBLC_CTYPE\fR category of the current locale is replaced in the returned
1239 1233 string by the lower-case letter specified by the mapping. Other characters in
1240 1234 \fIs\fR are unchanged in the returned string.
1241 1235 .RE
1242 1236
1243 1237 .sp
1244 1238 .ne 2
1245 1239 .na
1246 1240 \fB\fBtoupper\fR(\fIs\fR)\fR
1247 1241 .ad
1248 1242 .sp .6
1249 1243 .RS 4n
1250 1244 Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
1251 1245 a lower-case letter specified to have a \fBtoupper\fR mapping by the
1252 1246 \fBLC_CTYPE\fR category of the current locale is replaced in the returned
1253 1247 string by the upper-case letter specified by the mapping. Other characters in
1254 1248 \fIs\fR are unchanged in the returned string.
1255 1249 .RE
1256 1250
1257 1251 .sp
1258 1252 .LP
1259 1253 All of the preceding functions that take \fIERE\fR as a parameter expect a
1260 1254 pattern or a string valued expression that is a regular expression as defined
1261 1255 below.
1262 1256 .SS "Input/Output and General Functions"
1263 1257 .sp
1264 1258 .LP
1265 1259 The input/output and general functions are:
1266 1260 .sp
1267 1261 .ne 2
1268 1262 .na
1269 1263 \fB\fBclose(\fR\fIexpression\fR)\fR
1270 1264 .ad
1271 1265 .RS 27n
1272 1266 Close the file or pipe opened by a \fBprint\fR or \fBprintf\fR statement or a
1273 1267 call to \fBgetline\fR with the same string-valued \fIexpression\fR. If the
1274 1268 close was successful, the function returns \fB0\fR; otherwise, it returns
1275 1269 non-zero.
1276 1270 .RE
1277 1271
1278 1272 .sp
1279 1273 .ne 2
1280 1274 .na
1281 1275 \fB\fIexpression\fR|\fBgetline\fR[\fIvar\fR]\fR
1282 1276 .ad
1283 1277 .RS 27n
1284 1278 Read a record of input from a stream piped from the output of a command. The
1285 1279 stream is created if no stream is currently open with the value of
1286 1280 \fIexpression\fR as its command name. The stream created is equivalent to one
1287 1281 created by a call to the \fBpopen\fR function with the value of
1288 1282 \fIexpression\fR as the \fIcommand\fR argument and a value of \fBr\fR as the
1289 1283 \fImode\fR argument. As long as the stream remains open, subsequent calls in
1290 1284 which \fIexpression\fR evaluates to the same string value reads subsequent
1291 1285 records from the file. The stream remains open until the \fBclose\fR function
1292 1286 is called with an expression that evaluates to the same string value. At that
1293 1287 time, the stream is closed as if by a call to the \fBpclose\fR function. If
1294 1288 \fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise, \fIvar\fR is
1295 1289 set.
1296 1290 .sp
1297 1291 The \fBgetline\fR operator can form ambiguous constructs when there are
1298 1292 operators that are not in parentheses (including concatenate) to the left of
1299 1293 the \fB|\fR (to the beginning of the expression containing \fBgetline\fR). In
1300 1294 the context of the \fB$\fR operator, \fB|\fR behaves as if it had a lower
1301 1295 precedence than \fB$\fR. The result of evaluating other operators is
1302 1296 unspecified, and all such uses of portable applications must be put in
1303 1297 parentheses properly.
1304 1298 .RE
1305 1299
1306 1300 .sp
1307 1301 .ne 2
1308 1302 .na
1309 1303 \fB\fBgetline\fR\fR
1310 1304 .ad
1311 1305 .RS 30n
1312 1306 Set \fB$0\fR to the next input record from the current input file. This form of
1313 1307 \fBgetline\fR sets the \fBNF\fR, \fBNR\fR, and \fBFNR\fR variables.
1314 1308 .RE
1315 1309
1316 1310 .sp
1317 1311 .ne 2
1318 1312 .na
1319 1313 \fB\fBgetline\fR \fIvar\fR\fR
1320 1314 .ad
1321 1315 .RS 30n
1322 1316 Set variable \fIvar\fR to the next input record from the current input file.
1323 1317 This form of \fBgetline\fR sets the \fBFNR\fR and \fBNR\fR variables.
1324 1318 .RE
1325 1319
1326 1320 .sp
1327 1321 .ne 2
1328 1322 .na
1329 1323 \fB\fBgetline\fR [\fIvar\fR] \fB<\fR \fIexpression\fR\fR
1330 1324 .ad
1331 1325 .RS 30n
1332 1326 Read the next record of input from a named file. The \fIexpression\fR is
1333 1327 evaluated to produce a string that is used as a full pathname. If the file of
1334 1328 that name is not currently open, it is opened. As long as the stream remains
1335 1329 open, subsequent calls in which \fIexpression\fR evaluates to the same string
1336 1330 value reads subsequent records from the file. The file remains open until the
1337 1331 \fBclose\fR function is called with an expression that evaluates to the same
1338 1332 string value. If \fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise,
1339 1333 \fIvar\fR is set.
1340 1334 .sp
1341 1335 The \fBgetline\fR operator can form ambiguous constructs when there are binary
1342 1336 operators that are not in parentheses (including concatenate) to the right of
1343 1337 the \fB<\fR (up to the end of the expression containing the \fBgetline\fR). The
1344 1338 result of evaluating such a construct is unspecified, and all such uses of
1345 1339 portable applications must be put in parentheses properly.
1346 1340 .RE
1347 1341
1348 1342 .sp
1349 1343 .ne 2
1350 1344 .na
1351 1345 \fB\fBsystem\fR(\fIexpression\fR)\fR
1352 1346 .ad
1353 1347 .RS 30n
1354 1348 Execute the command given by \fIexpression\fR in a manner equivalent to the
1355 1349 \fBsystem\fR(3C) function and return the exit status of the command.
1356 1350 .RE
1357 1351
1358 1352 .sp
1359 1353 .LP
1360 1354 All forms of \fBgetline\fR return \fB1\fR for successful input, \fB0\fR for end
1361 1355 of file, and \fB\(mi1\fR for an error.
1362 1356 .sp
1363 1357 .LP
1364 1358 Where strings are used as the name of a file or pipeline, the strings must be
1365 1359 textually identical. The terminology ``same string value'' implies that
1366 1360 ``equivalent strings'', even those that differ only by space characters,
1367 1361 represent different files.
1368 1362 .SS "User-defined Functions"
1369 1363 .sp
1370 1364 .LP
1371 1365 The \fBnawk\fR language also provides user-defined functions. Such functions
1372 1366 can be defined as:
1373 1367 .sp
1374 1368 .in +2
1375 1369 .nf
1376 1370 \fBfunction\fR \fIname\fR(\fIargs\fR,\|.\|.\|.) { \fIstatements\fR }
1377 1371 .fi
1378 1372 .in -2
1379 1373
1380 1374 .sp
1381 1375 .LP
1382 1376 A function can be referred to anywhere in an \fBnawk\fR program; in particular,
1383 1377 its use can precede its definition. The scope of a function is global.
1384 1378 .sp
1385 1379 .LP
1386 1380 Function arguments can be either scalars or arrays; the behavior is undefined
1387 1381 if an array name is passed as an argument that the function uses as a scalar,
1388 1382 or if a scalar expression is passed as an argument that the function uses as an
1389 1383 array. Function arguments are passed by value if scalar and by reference if
1390 1384 array name. Argument names are local to the function; all other variable names
1391 1385 are global. The same name is not used as both an argument name and as the name
1392 1386 of a function or a special \fBnawk\fR variable. The same name must not be used
1393 1387 both as a variable name with global scope and as the name of a function. The
1394 1388 same name must not be used within the same scope both as a scalar variable and
1395 1389 as an array.
1396 1390 .sp
1397 1391 .LP
1398 1392 The number of parameters in the function definition need not match the number
1399 1393 of parameters in the function call. Excess formal parameters can be used as
1400 1394 local variables. If fewer arguments are supplied in a function call than are in
1401 1395 the function definition, the extra parameters that are used in the function
1402 1396 body as scalars are initialized with a string value of the null string and a
1403 1397 numeric value of zero, and the extra parameters that are used in the function
1404 1398 body as arrays are initialized as empty arrays. If more arguments are supplied
1405 1399 in a function call than are in the function definition, the behavior is
1406 1400 undefined.
1407 1401 .sp
1408 1402 .LP
1409 1403 When invoking a function, no white space can be placed between the function
1410 1404 name and the opening parenthesis. Function calls can be nested and recursive
1411 1405 calls can be made upon functions. Upon return from any nested or recursive
1412 1406 function call, the values of all of the calling function's parameters are
1413 1407 unchanged, except for array parameters passed by reference. The \fBreturn\fR
1414 1408 statement can be used to return a value. If a \fBreturn\fR statement appears
1415 1409 outside of a function definition, the behavior is undefined.
1416 1410 .sp
1417 1411 .LP
1418 1412 In the function definition, newline characters are optional before the opening
1419 1413 brace and after the closing brace. Function definitions can appear anywhere in
1420 1414 the program where a \fIpattern-action\fR pair is allowed.
1421 1415 .SH USAGE
1422 1416 .sp
1423 1417 .LP
1424 1418 The \fBindex\fR, \fBlength\fR, \fBmatch\fR, and \fBsubstr\fR functions should
1425 1419 not be confused with similar functions in the \fBISO C\fR standard; the
1426 1420 \fBnawk\fR versions deal with characters, while the \fBISO C\fR standard deals
1427 1421 with bytes.
1428 1422 .sp
1429 1423 .LP
1430 1424 Because the concatenation operation is represented by adjacent expressions
1431 1425 rather than an explicit operator, it is often necessary to use parentheses to
1432 1426 enforce the proper evaluation precedence.
1433 1427 .sp
1434 1428 .LP
1435 1429 See \fBlargefile\fR(5) for the description of the behavior of \fBnawk\fR when
1436 1430 encountering files greater than or equal to 2 Gbyte (2^31 bytes).
1437 1431 .SH EXAMPLES
1438 1432 .sp
1439 1433 .LP
1440 1434 The \fBnawk\fR program specified in the command line is most easily specified
1441 1435 within single-quotes (for example, \fB\&'\fR\fIprogram\fR\fB\&'\fR) for
1442 1436 applications using \fBsh\fR, because \fBnawk\fR programs commonly contain
1443 1437 characters that are special to the shell, including double-quotes. In the cases
1444 1438 where a \fBnawk\fR program contains single-quote characters, it is usually
1445 1439 easiest to specify most of the program as strings within single-quotes
1446 1440 concatenated by the shell with quoted single-quote characters. For example:
1447 1441 .sp
1448 1442 .in +2
1449 1443 .nf
1450 1444 nawk '/'\e''/ { print "quote:", $0 }'
1451 1445 .fi
1452 1446 .in -2
1453 1447
1454 1448 .sp
1455 1449 .LP
1456 1450 prints all lines from the standard input containing a single-quote character,
1457 1451 prefixed with \fBquote:\fR.
1458 1452 .sp
1459 1453 .LP
1460 1454 The following are examples of simple \fBnawk\fR programs:
1461 1455 .LP
1462 1456 \fBExample 1 \fRWrite to the standard output all input lines for which field 3
1463 1457 is greater than 5:
1464 1458 .sp
1465 1459 .in +2
1466 1460 .nf
1467 1461 \fB$3 > 5\fR
1468 1462 .fi
1469 1463 .in -2
1470 1464 .sp
1471 1465
1472 1466 .LP
1473 1467 \fBExample 2 \fRWrite every tenth line:
1474 1468 .sp
1475 1469 .in +2
1476 1470 .nf
1477 1471 \fB(NR % 10) == 0\fR
1478 1472 .fi
1479 1473 .in -2
1480 1474 .sp
1481 1475
1482 1476 .LP
1483 1477 \fBExample 3 \fRWrite any line with a substring matching the regular
1484 1478 expression:
1485 1479 .sp
1486 1480 .in +2
1487 1481 .nf
1488 1482 \fB/(G|D)(2[0-9][[:alpha:]]*)/\fR
1489 1483 .fi
1490 1484 .in -2
1491 1485 .sp
1492 1486
1493 1487 .LP
1494 1488 \fBExample 4 \fRPrint any line with a substring containing a G or D, followed
1495 1489 by a sequence of digits and characters:
1496 1490 .sp
1497 1491 .LP
1498 1492 This example uses character classes \fBdigit\fR and \fBalpha\fR to match
1499 1493 language-independent digit and alphabetic characters, respectively.
1500 1494
1501 1495 .sp
1502 1496 .in +2
1503 1497 .nf
1504 1498 \fB/(G|D)([[:digit:][:alpha:]]*)/\fR
1505 1499 .fi
1506 1500 .in -2
1507 1501 .sp
1508 1502
1509 1503 .LP
1510 1504 \fBExample 5 \fRWrite any line in which the second field matches the regular
1511 1505 expression and the fourth field does not:
1512 1506 .sp
1513 1507 .in +2
1514 1508 .nf
1515 1509 \fB$2 ~ /xyz/ && $4 !~ /xyz/\fR
1516 1510 .fi
1517 1511 .in -2
1518 1512 .sp
1519 1513
1520 1514 .LP
1521 1515 \fBExample 6 \fRWrite any line in which the second field contains a backslash:
1522 1516 .sp
1523 1517 .in +2
1524 1518 .nf
1525 1519 \fB$2 ~ /\e\e/\fR
1526 1520 .fi
1527 1521 .in -2
1528 1522 .sp
1529 1523
1530 1524 .LP
1531 1525 \fBExample 7 \fRWrite any line in which the second field contains a backslash
1532 1526 (alternate method):
1533 1527 .sp
1534 1528 .LP
1535 1529 Notice that backslash escapes are interpreted twice, once in lexical processing
1536 1530 of the string and once in processing the regular expression.
1537 1531
1538 1532 .sp
1539 1533 .in +2
1540 1534 .nf
1541 1535 \fB$2 ~ "\e\e\e\e"\fR
1542 1536 .fi
1543 1537 .in -2
1544 1538 .sp
1545 1539
1546 1540 .LP
1547 1541 \fBExample 8 \fRWrite the second to the last and the last field in each line,
1548 1542 separating the fields by a colon:
1549 1543 .sp
1550 1544 .in +2
1551 1545 .nf
1552 1546 \fB{OFS=":";print $(NF-1), $NF}\fR
1553 1547 .fi
1554 1548 .in -2
1555 1549 .sp
1556 1550
1557 1551 .LP
1558 1552 \fBExample 9 \fRWrite the line number and number of fields in each line:
1559 1553 .sp
1560 1554 .LP
1561 1555 The three strings representing the line number, the colon and the number of
1562 1556 fields are concatenated and that string is written to standard output.
1563 1557
1564 1558 .sp
1565 1559 .in +2
1566 1560 .nf
1567 1561 \fB{print NR ":" NF}\fR
1568 1562 .fi
1569 1563 .in -2
1570 1564 .sp
1571 1565
1572 1566 .LP
1573 1567 \fBExample 10 \fRWrite lines longer than 72 characters:
1574 1568 .sp
1575 1569 .in +2
1576 1570 .nf
1577 1571 \fB{length($0) > 72}\fR
1578 1572 .fi
1579 1573 .in -2
1580 1574 .sp
1581 1575
1582 1576 .LP
1583 1577 \fBExample 11 \fRWrite first two fields in opposite order separated by the OFS:
1584 1578 .sp
1585 1579 .in +2
1586 1580 .nf
1587 1581 \fB{ print $2, $1 }\fR
1588 1582 .fi
1589 1583 .in -2
1590 1584 .sp
1591 1585
1592 1586 .LP
1593 1587 \fBExample 12 \fRSame, with input fields separated by comma or space and tab
1594 1588 characters, or both:
1595 1589 .sp
1596 1590 .in +2
1597 1591 .nf
1598 1592 \fBBEGIN { FS = ",[\et]*|[\et]+" }
1599 1593 { print $2, $1 }\fR
1600 1594 .fi
1601 1595 .in -2
1602 1596 .sp
1603 1597
1604 1598 .LP
1605 1599 \fBExample 13 \fRAdd up first column, print sum and average:
1606 1600 .sp
1607 1601 .in +2
1608 1602 .nf
1609 1603 \fB{s += $1 }
1610 1604 END {print "sum is ", s, " average is", s/NR}\fR
1611 1605 .fi
1612 1606 .in -2
1613 1607 .sp
1614 1608
1615 1609 .LP
1616 1610 \fBExample 14 \fRWrite fields in reverse order, one per line (many lines out
1617 1611 for each line in):
1618 1612 .sp
1619 1613 .in +2
1620 1614 .nf
1621 1615 \fB{ for (i = NF; i > 0; --i) print $i }\fR
1622 1616 .fi
1623 1617 .in -2
1624 1618 .sp
1625 1619
1626 1620 .LP
1627 1621 \fBExample 15 \fRWrite all lines between occurrences of the strings "start" and
1628 1622 "stop":
1629 1623 .sp
1630 1624 .in +2
1631 1625 .nf
1632 1626 \fB/start/, /stop/\fR
1633 1627 .fi
1634 1628 .in -2
1635 1629 .sp
1636 1630
1637 1631 .LP
1638 1632 \fBExample 16 \fRWrite all lines whose first field is different from the
1639 1633 previous one:
1640 1634 .sp
1641 1635 .in +2
1642 1636 .nf
1643 1637 \fB$1 != prev { print; prev = $1 }\fR
1644 1638 .fi
1645 1639 .in -2
1646 1640 .sp
1647 1641
1648 1642 .LP
1649 1643 \fBExample 17 \fRSimulate the echo command:
1650 1644 .sp
1651 1645 .in +2
1652 1646 .nf
1653 1647 \fBBEGIN {
1654 1648 for (i = 1; i < ARGC; ++i)
1655 1649 printf "%s%s", ARGV[i], i==ARGC-1?"\en":""
1656 1650 }\fR
1657 1651 .fi
1658 1652 .in -2
1659 1653 .sp
1660 1654
1661 1655 .LP
1662 1656 \fBExample 18 \fRWrite the path prefixes contained in the PATH environment
1663 1657 variable, one per line:
1664 1658 .sp
1665 1659 .in +2
1666 1660 .nf
1667 1661 \fBBEGIN {
1668 1662 n = split (ENVIRON["PATH"], path, ":")
1669 1663 for (i = 1; i <= n; ++i)
1670 1664 print path[i]
1671 1665 }\fR
1672 1666 .fi
1673 1667 .in -2
1674 1668 .sp
1675 1669
1676 1670 .LP
1677 1671 \fBExample 19 \fRPrint the file "input", filling in page numbers starting at 5:
1678 1672 .sp
1679 1673 .LP
1680 1674 If there is a file named \fBinput\fR containing page headers of the form
1681 1675
1682 1676 .sp
1683 1677 .in +2
1684 1678 .nf
1685 1679 Page#
1686 1680 .fi
1687 1681 .in -2
1688 1682
1689 1683 .sp
1690 1684 .LP
1691 1685 and a file named \fBprogram\fR that contains
1692 1686
1693 1687 .sp
1694 1688 .in +2
1695 1689 .nf
1696 1690 /Page/{ $2 = n++; }
1697 1691 { print }
1698 1692 .fi
1699 1693 .in -2
1700 1694
1701 1695 .sp
1702 1696 .LP
1703 1697 then the command line
1704 1698
1705 1699 .sp
1706 1700 .in +2
1707 1701 .nf
1708 1702 \fBnawk -f program n=5 input\fR
1709 1703 .fi
1710 1704 .in -2
1711 1705 .sp
1712 1706
1713 1707 .sp
1714 1708 .LP
1715 1709 prints the file \fBinput\fR, filling in page numbers starting at 5.
1716 1710
1717 1711 .SH ENVIRONMENT VARIABLES
1718 1712 .sp
1719 1713 .LP
1720 1714 See \fBenviron\fR(5) for descriptions of the following environment variables
1721 1715 that affect execution: \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and
1722 1716 \fBNLSPATH\fR.
1723 1717 .sp
1724 1718 .ne 2
1725 1719 .na
1726 1720 \fB\fBLC_NUMERIC\fR\fR
1727 1721 .ad
1728 1722 .RS 14n
1729 1723 Determine the radix character used when interpreting numeric input, performing
1730 1724 conversions between numeric and string values and formatting numeric output.
1731 1725 Regardless of locale, the period character (the decimal-point character of the
1732 1726 POSIX locale) is the decimal-point character recognized in processing \fBawk\fR
1733 1727 programs (including assignments in command-line arguments).
1734 1728 .RE
1735 1729
1736 1730 .SH EXIT STATUS
1737 1731 .sp
1738 1732 .LP
1739 1733 The following exit values are returned:
1740 1734 .sp
1741 1735 .ne 2
1742 1736 .na
1743 1737 \fB\fB0\fR\fR
1744 1738 .ad
1745 1739 .RS 6n
1746 1740 All input files were processed successfully.
1747 1741 .RE
1748 1742
1749 1743 .sp
1750 1744 .ne 2
1751 1745 .na
1752 1746 \fB\fB>0\fR\fR
1753 1747 .ad
1754 1748 .RS 6n
1755 1749 An error occurred.
1756 1750 .RE
1757 1751
1758 1752 .sp
1759 1753 .LP
1760 1754 The exit status can be altered within the program by using an \fBexit\fR
1761 1755 expression.
1762 1756
1763 1757 .SH SEE ALSO
1764 1758 .sp
1765 1759 .LP
1766 1760 \fBawk\fR(1), \fBed\fR(1), \fBegrep\fR(1), \fBgrep\fR(1), \fBlex\fR(1),
1767 1761 \fBsed\fR(1), \fBpopen\fR(3C), \fBprintf\fR(3C), \fBsystem\fR(3C),
1768 1762 \fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5),
1769 1763 \fBXPG4\fR(5)
1770 1764 .sp
1771 1765 .LP
1772 1766 Aho, A. V., B. W. Kernighan, and P. J. Weinberger, \fIThe AWK Programming
1773 1767 Language\fR, Addison-Wesley, 1988.
1774 1768 .SH DIAGNOSTICS
1775 1769 .sp
1776 1770 .LP
1777 1771 If any \fIfile\fR operand is specified and the named file cannot be accessed,
1778 1772 \fBnawk\fR writes a diagnostic message to standard error and terminate without
1779 1773 any further action.
1780 1774 .sp
1781 1775 .LP
1782 1776 If the program specified by either the \fIprogram\fR operand or a
1783 1777 \fIprogfile\fR operand is not a valid \fBnawk\fR program (as specified in
1784 1778 \fBEXTENDED DESCRIPTION\fR), the behavior is undefined.
1785 1779 .SH NOTES
1786 1780 .sp
1787 1781 .LP
1788 1782 Input white space is not preserved on output if fields are involved.
1789 1783 .sp
1790 1784 .LP
1791 1785 There are no explicit conversions between numbers and strings. To force an
1792 1786 expression to be treated as a number add 0 to it; to force it to be treated as
1793 1787 a string concatenate the null string (\fB""\fR) to it.
↓ open down ↓ |
1752 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX