| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | .\" $NetBSD: awk.1,v 1.18 2009/03/09 14:10:12 joerg Exp $ | | 1 | .\" $NetBSD: awk.1,v 1.19 2009/03/15 16:21:46 joerg Exp $ |
2 | .\" | | 2 | .\" |
3 | .\" Copyright (C) Lucent Technologies 1997 | | 3 | .\" Copyright (C) Lucent Technologies 1997 |
4 | .\" All Rights Reserved | | 4 | .\" All Rights Reserved |
5 | .\" | | 5 | .\" |
6 | .\" Permission to use, copy, modify, and distribute this software and | | 6 | .\" Permission to use, copy, modify, and distribute this software and |
7 | .\" its documentation for any purpose and without fee is hereby | | 7 | .\" its documentation for any purpose and without fee is hereby |
8 | .\" granted, provided that the above copyright notice appear in all | | 8 | .\" granted, provided that the above copyright notice appear in all |
9 | .\" copies and that both that the copyright notice and this | | 9 | .\" copies and that both that the copyright notice and this |
10 | .\" permission notice and warranty disclaimer appear in supporting | | 10 | .\" permission notice and warranty disclaimer appear in supporting |
11 | .\" documentation, and that the name Lucent Technologies or any of | | 11 | .\" documentation, and that the name Lucent Technologies or any of |
12 | .\" its entities not be used in advertising or publicity pertaining | | 12 | .\" its entities not be used in advertising or publicity pertaining |
13 | .\" to distribution of the software without specific, written prior | | 13 | .\" to distribution of the software without specific, written prior |
14 | .\" permission. | | 14 | .\" permission. |
| @@ -119,42 +119,43 @@ An input line is normally made up of fie | | | @@ -119,42 +119,43 @@ An input line is normally made up of fie |
119 | or by regular expression | | 119 | or by regular expression |
120 | .Va FS . | | 120 | .Va FS . |
121 | The fields are denoted | | 121 | The fields are denoted |
122 | .Va $1 , | | 122 | .Va $1 , |
123 | .Va $2 , | | 123 | .Va $2 , |
124 | \&..., while | | 124 | \&..., while |
125 | .Va $0 | | 125 | .Va $0 |
126 | refers to the entire line. | | 126 | refers to the entire line. |
127 | If | | 127 | If |
128 | .Va FS | | 128 | .Va FS |
129 | is null, the input line is split into one field per character. | | 129 | is null, the input line is split into one field per character. |
130 | .Pp | | 130 | .Pp |
131 | A pattern-action statement has the form | | 131 | A pattern-action statement has the form |
132 | .sp | | 132 | .Lp |
133 | .Dl pattern \&{ action \&} | | 133 | .Dl pattern \&{ action \&} |
134 | .sp | | 134 | .Lp |
135 | A missing \&{ action \&} | | 135 | A missing \&{ action \&} |
136 | means print the line; | | 136 | means print the line; |
137 | a missing pattern always matches. | | 137 | a missing pattern always matches. |
138 | Pattern-action statements are separated by newlines or semicolons. | | 138 | Pattern-action statements are separated by newlines or semicolons. |
139 | .Pp | | 139 | .Pp |
140 | An action is a sequence of statements. | | 140 | An action is a sequence of statements. |
141 | Statements are terminated by | | 141 | Statements are terminated by |
142 | semicolons, newlines or right braces. | | 142 | semicolons, newlines or right braces. |
143 | An empty | | 143 | An empty |
144 | .Ar expression-list | | 144 | .Ar expression-list |
145 | stands for | | 145 | stands for |
146 | .Va $0 . | | 146 | .Va $0 . |
147 | String constants are quoted \&\f(CW"\ "\fR, | | 147 | String constants are quoted |
| | | 148 | .Em \&"\ \&" , |
148 | with the usual C escapes recognized within. | | 149 | with the usual C escapes recognized within. |
149 | Expressions take on string or numeric values as appropriate, | | 150 | Expressions take on string or numeric values as appropriate, |
150 | and are built using the | | 151 | and are built using the |
151 | .Sx Operators | | 152 | .Sx Operators |
152 | (see next subsection). | | 153 | (see next subsection). |
153 | Variables may be scalars, array elements | | 154 | Variables may be scalars, array elements |
154 | (denoted | | 155 | (denoted |
155 | .Va x[i] ) | | 156 | .Va x[i] ) |
156 | or fields. | | 157 | or fields. |
157 | Variables are initialized to the null string. | | 158 | Variables are initialized to the null string. |
158 | Array subscripts may be any string, | | 159 | Array subscripts may be any string, |
159 | not necessarily numeric; | | 160 | not necessarily numeric; |
160 | this allows for a form of associative memory. | | 161 | this allows for a form of associative memory. |
| @@ -193,154 +194,127 @@ String concatenation. | | | @@ -193,154 +194,127 @@ String concatenation. |
193 | .It Ic != == | | 194 | .It Ic != == |
194 | Regular relational operators | | 195 | Regular relational operators |
195 | .It Ic ~ !~ | | 196 | .It Ic ~ !~ |
196 | Regular expression match and not match | | 197 | Regular expression match and not match |
197 | .It Ic in | | 198 | .It Ic in |
198 | Array membership | | 199 | Array membership |
199 | .It Ic "\*[Am]\*[Am]" | | 200 | .It Ic "\*[Am]\*[Am]" |
200 | Logical AND | | 201 | Logical AND |
201 | .It Ic "||" | | 202 | .It Ic "||" |
202 | Logical OR | | 203 | Logical OR |
203 | .It Ic ?: | | 204 | .It Ic ?: |
204 | C conditional expression. | | 205 | C conditional expression. |
205 | This is used as | | 206 | This is used as |
206 | .Ar expr1 Ic ? Ar expr2 Ic : Ar expr3 No . | | 207 | .Ar expr1 Ic \&? Ar expr2 Ic \&: Ar expr3 No . |
207 | If | | 208 | If |
208 | .Ar expr1 | | 209 | .Ar expr1 |
209 | is true, the result value is | | 210 | is true, the result value is |
210 | .Ar expr2 , | | 211 | .Ar expr2 , |
211 | otherwise it is | | 212 | otherwise it is |
212 | .Ar expr3 . | | 213 | .Ar expr3 . |
213 | Only one of | | 214 | Only one of |
214 | .Ar expr2 | | 215 | .Ar expr2 |
215 | and | | 216 | and |
216 | .Ar expr3 | | 217 | .Ar expr3 |
217 | is evaluated. | | 218 | is evaluated. |
218 | .It Ic = += -= | | 219 | .It Ic = += -= |
219 | .It Ic *= /= %= ^= | | 220 | .It Ic *= /= %= ^= |
220 | Assignment and Operator-Assignment | | 221 | Assignment and Operator-Assignment |
221 | .El | | 222 | .El |
222 | .Ss Control Statements | | 223 | .Ss Control Statements |
223 | The control statements are as follows: | | 224 | The control statements are as follows: |
224 | .Pp | | 225 | .Pp |
225 | .Bl -hang -offset indent -width indent -compact | | 226 | .Bl -hang -offset indent -width indent -compact |
226 | .It Xo | | 227 | .It Ic if \&( Ar expression Ic \&) Ar statement Bq Ic else Ar statement |
227 | .Ic if \&( Ar expression Ic \&) Ar statement | | 228 | .It Ic while \&( Ar expression Ic \&) Ar statement |
228 | .Bq Ic else Ar statement | | 229 | .It Ic for \&( Ar expression Ic \&; Ar expression Ic \&; \ |
229 | .Xc | | 230 | Ar expression Ic \&) Ar statement |
230 | .It Ic while( Ar expression Ic \&) Ar statement | | 231 | .It Ic for \&( Va var Ic in Ar array Ic \&) Ar statement |
231 | .It Xo | | 232 | .It Ic do Ar statement Ic while \&( Ar expression Ic \&) |
232 | .Ic for( Ar expression Ic \&; Ar expression Ic \&; | | | |
233 | .Ar expression Ic \&) | | | |
234 | .Ar statement | | | |
235 | .Xc | | | |
236 | .It Xo | | | |
237 | .Ic for( Va var Ic in Ar array | | | |
238 | .Ic \&) | | | |
239 | .Ar statement | | | |
240 | .Xc | | | |
241 | .It Xo | | | |
242 | .Ic do Ar statement | | | |
243 | .Ic while( Ar expression Ic \&) | | | |
244 | .Xc | | | |
245 | .It Ic break | | 233 | .It Ic break |
246 | .It Ic continue | | 234 | .It Ic continue |
247 | .It Ic delete Va array Bq Ar expression | | 235 | .It Ic delete Va array Bq Ar expression |
248 | .It Ic delete Va array | | 236 | .It Ic delete Va array |
249 | .It Ic exit Bq Ar expression | | 237 | .It Ic exit Bq Ar expression |
250 | .Ar expression | | 238 | .Ar expression |
251 | .It Ic return Bq Ar expression | | 239 | .It Ic return Bq Ar expression |
252 | .It Ic { Ar [ statement ... ] Ic } | | 240 | .It Ic \&{ Ar [ statement ... ] Ic \&} |
253 | .El | | 241 | .El |
254 | .Ss I/O Statements | | 242 | .Ss I/O Statements |
255 | The input/output statements are as follows: | | 243 | The input/output statements are as follows: |
256 | .Pp | | 244 | .Pp |
257 | .Bl -tag -width indent | | 245 | .Bl -tag -width indent |
258 | .It Fn close expr | | 246 | .It Fn close expr |
259 | Closes the file or pipe | | 247 | Closes the file or pipe |
260 | .Ar expr . | | 248 | .Ar expr . |
261 | Returns zero on success; otherwise nonzero. | | 249 | Returns zero on success; otherwise nonzero. |
262 | .It Fn fflush expr | | 250 | .It Fn fflush expr |
263 | Flushes any buffered output for the file or pipe | | 251 | Flushes any buffered output for the file or pipe |
264 | .Ar expr . | | 252 | .Ar expr . |
265 | Returns zero on success; otherwise nonzero. | | 253 | Returns zero on success; otherwise nonzero. |
266 | .It Ic getline Bq Va var | | 254 | .It Ic getline Bq Va var |
267 | Set | | 255 | Set |
268 | .Va var | | 256 | .Va var |
269 | (or | | 257 | (or |
270 | .Va $0 if | | 258 | .Va $0 if |
271 | .Va var | | 259 | .Va var |
272 | is not specified) | | 260 | is not specified) |
273 | to the next input record from the current input file. | | 261 | to the next input record from the current input file. |
274 | .Ic getline | | 262 | .Ic getline |
275 | returns 1 for a successful input, | | 263 | returns 1 for a successful input, |
276 | 0 for end of file, and \-1 for an error. | | 264 | 0 for end of file, and \-1 for an error. |
277 | .It Xo | | 265 | .It Ic getline Bo Va var Bc Ic \*[Lt] Ar file |
278 | .Ic getline | | | |
279 | .Bq Va var | | | |
280 | .Ic \*[Lt] | | | |
281 | .Ar file | | | |
282 | .Xc | | | |
283 | Set | | 266 | Set |
284 | .Va var | | 267 | .Va var |
285 | (or | | 268 | (or |
286 | .Va $0 if | | 269 | .Va $0 if |
287 | .Va var | | 270 | .Va var |
288 | is not specified) | | 271 | is not specified) |
289 | to the next input record from the specified file | | 272 | to the next input record from the specified file |
290 | .Ar file . | | 273 | .Ar file . |
291 | .It Ar expr Ic \&| getline | | 274 | .It Ar expr Ic \&| getline |
292 | Pipes the output of | | 275 | Pipes the output of |
293 | .Ar expr | | 276 | .Ar expr |
294 | into | | 277 | into |
295 | .Ic getline ; | | 278 | .Ic getline ; |
296 | each call of | | 279 | each call of |
297 | .Ic getline | | 280 | .Ic getline |
298 | returns the next line of output from | | 281 | returns the next line of output from |
299 | .Ar expr . | | 282 | .Ar expr . |
300 | .It Ic next | | 283 | .It Ic next |
301 | Skip remaining patterns on this input line. | | 284 | Skip remaining patterns on this input line. |
302 | .It Ic nextfile | | 285 | .It Ic nextfile |
303 | Skip rest of this file, open next, start at top. | | 286 | Skip rest of this file, open next, start at top. |
304 | .It Xo | | 287 | .It Ic print Bo Ar expr-list Bc Bq Ic \*[Gt] Ar file |
305 | .Ic print | | | |
306 | .Bq Ar expr-list | | | |
307 | .Bq Ic \*[Gt] Ar file | | | |
308 | .Xc | | | |
309 | The | | 288 | The |
310 | .Ic print | | 289 | .Ic print |
311 | statement prints its arguments on the standard output (or to a file | | 290 | statement prints its arguments on the standard output (or to a file |
312 | if | | 291 | if |
313 | .Ic \*[Gt] file | | 292 | .Ic \*[Gt] file |
314 | or to a pipe if | | 293 | or to a pipe if |
315 | .Ic | Ar expr | | 294 | .Ic | Ar expr |
316 | is present), | | 295 | is present), |
317 | separated by the current output field separator | | 296 | separated by the current output field separator |
318 | .Va OFS , | | 297 | .Va OFS , |
319 | and terminated by the | | 298 | and terminated by the |
320 | output record separator | | 299 | output record separator |
321 | .Va ORS . | | 300 | .Va ORS . |
322 | Both | | 301 | Both |
323 | .Ar file | | 302 | .Ar file |
324 | and | | 303 | and |
325 | .Ar expr | | 304 | .Ar expr |
326 | may be literal names or parenthesized expressions; identical string values in | | 305 | may be literal names or parenthesized expressions; identical string values in |
327 | different statements denote the same open file. | | 306 | different statements denote the same open file. |
328 | .It Xo | | 307 | .It Ic printf Ar format Bo Ic \&, Ar expr-list Bc Bq Ic \*[Gt] Ar file |
329 | .Ic printf | | | |
330 | .Ar format | | | |
331 | .Bq Ic , Ar expr-list | | | |
332 | .Bq Ic \*[Gt] Ar file | | | |
333 | .Xc | | | |
334 | Format and print its expression list according to | | 308 | Format and print its expression list according to |
335 | .Ar format . | | 309 | .Ar format . |
336 | See | | 310 | See |
337 | .Xr printf 3 | | 311 | .Xr printf 3 |
338 | for list of supported formats and their meaning. | | 312 | for list of supported formats and their meaning. |
339 | .El | | 313 | .El |
340 | .Ss Mathematical and Numeric Functions | | 314 | .Ss Mathematical and Numeric Functions |
341 | AWK has the following mathematical and numerical functions built-in: | | 315 | AWK has the following mathematical and numerical functions built-in: |
342 | .Pp | | 316 | .Pp |
343 | .Bl -tag -width indent | | 317 | .Bl -tag -width indent |
344 | .It Fn atan2 x y | | 318 | .It Fn atan2 x y |
345 | Returns the arctangent of | | 319 | Returns the arctangent of |
346 | .Ar x Ic / Ar y | | 320 | .Ar x Ic / Ar y |
| @@ -691,37 +665,35 @@ Functions may be defined (at the positio | | | @@ -691,37 +665,35 @@ Functions may be defined (at the positio |
691 | .Ic function foo(a, b, c) { ...; return x } | | 665 | .Ic function foo(a, b, c) { ...; return x } |
692 | .Ed | | 666 | .Ed |
693 | .Pp | | 667 | .Pp |
694 | Parameters are passed by value if scalar and by reference if array name; | | 668 | Parameters are passed by value if scalar and by reference if array name; |
695 | functions may be called recursively. | | 669 | functions may be called recursively. |
696 | Parameters are local to the function; all other variables are global. | | 670 | Parameters are local to the function; all other variables are global. |
697 | Thus local variables may be created by providing excess parameters in | | 671 | Thus local variables may be created by providing excess parameters in |
698 | the function definition. | | 672 | the function definition. |
699 | .Sh EXAMPLES | | 673 | .Sh EXAMPLES |
700 | .Bl -tag -width indent -compact | | 674 | .Bl -tag -width indent -compact |
701 | .It Ic length($0) \*[Gt] 72 | | 675 | .It Ic length($0) \*[Gt] 72 |
702 | Print lines longer than 72 characters. | | 676 | Print lines longer than 72 characters. |
703 | .Pp | | 677 | .Pp |
704 | .It Ic { print $2, $1 } | | 678 | .It Ic \&{ print $2, $1 \&} |
705 | Print first two fields in opposite order. | | 679 | Print first two fields in opposite order. |
706 | .Pp | | 680 | .Pp |
707 | .It Ic BEGIN { FS = \&",[ \et]*|[ \et]+\&" } | | 681 | .It Ic BEGIN { FS = \&",[ \et]*|[ \et]+\&" } |
708 | .It Ic "\ \ \ \ \ \ {" print \&$2, \&$1 } | | 682 | .It Ic "\ \ \ \ \ \ {" print \&$2, \&$1 } |
709 | Same, with input fields separated by comma and/or blanks and tabs. | | 683 | Same, with input fields separated by comma and/or blanks and tabs. |
710 | .Pp | | 684 | .Pp |
711 | .It Ic "\ \ \ \ {" s += $1 } | | 685 | .It Ic "\ \ \ \ {" s += $1 } |
712 | .It Xo | | 686 | .It Ic END { print \&"sum is\&", s, \&" average is\ \&",\ s/NR\ } |
713 | .Ic END { print \&"sum is\&", s, \&" average is\ \&",\ s/NR\ } | | | |
714 | .Xc | | | |
715 | Add up first column, print sum and average. | | 687 | Add up first column, print sum and average. |
716 | .Pp | | 688 | .Pp |
717 | .It Ic /start/, /stop/ | | 689 | .It Ic /start/, /stop/ |
718 | Print all lines between start/stop pairs. | | 690 | Print all lines between start/stop pairs. |
719 | .Pp | | 691 | .Pp |
720 | .It Ic BEGIN { # Simulate echo(1) | | 692 | .It Ic BEGIN { # Simulate echo(1) |
721 | .It Ic "\ \ \ \ " for (i = 1; i \*[Lt] ARGC;\ i++)\ printf\ \&"%s\ \&",\ ARGV[i] | | 693 | .It Ic "\ \ \ \ " for (i = 1; i \*[Lt] ARGC;\ i++)\ printf\ \&"%s\ \&",\ ARGV[i] |
722 | .It Ic "\ \ \ \ " printf \&"\en\&" | | 694 | .It Ic "\ \ \ \ " printf \&"\en\&" |
723 | .It Ic "\ \ \ \ " exit } | | 695 | .It Ic "\ \ \ \ " exit } |
724 | .El | | 696 | .El |
725 | .Sh SEE ALSO | | 697 | .Sh SEE ALSO |
726 | .Xr egrep 1 , | | 698 | .Xr egrep 1 , |
727 | .Xr lex 1 , | | 699 | .Xr lex 1 , |
| @@ -745,16 +717,16 @@ Edition 1.0, published by the Free Softw | | | @@ -745,16 +717,16 @@ Edition 1.0, published by the Free Softw |
745 | .Sh HISTORY | | 717 | .Sh HISTORY |
746 | .Nm nawk | | 718 | .Nm nawk |
747 | has been the default system | | 719 | has been the default system |
748 | .Nm | | 720 | .Nm |
749 | since | | 721 | since |
750 | .Nx 2.0 , | | 722 | .Nx 2.0 , |
751 | replacing the previously used GNU | | 723 | replacing the previously used GNU |
752 | .Nm . | | 724 | .Nm . |
753 | .Sh BUGS | | 725 | .Sh BUGS |
754 | There are no explicit conversions between numbers and strings. | | 726 | There are no explicit conversions between numbers and strings. |
755 | To force an expression to be treated as a number add 0 to it; | | 727 | To force an expression to be treated as a number add 0 to it; |
756 | to force it to be treated as a string concatenate | | 728 | to force it to be treated as a string concatenate |
757 | \&"\&" to it. | | 729 | \&"\&" to it. |
758 | .br | | 730 | .Pp |
759 | The scope rules for variables in functions are a botch; | | 731 | The scope rules for variables in functions are a botch; |
760 | the syntax is worse. | | 732 | the syntax is worse. |