libedit: Adjustments after the import
[dragonfly.git] / contrib / awk / awk.1
1 .de EX
2 .nf
3 .ft CW
4 ..
5 .de EE
6 .br
7 .fi
8 .ft 1
9 ..
10 .de TF
11 .IP "" "\w'\fB\\$1\ \ \fP'u"
12 .PD 0
13 ..
14 .TH AWK 1
15 .CT 1 files prog_other
16 .SH NAME
17 awk \- pattern-directed scanning and processing language
18 .SH SYNOPSIS
19 .B awk
20 [
21 .BI \-F
22 .I fs
23 ]
24 [
25 .BI \-v
26 .I var=value
27 ]
28 [
29 .I 'prog'
30 |
31 .BI \-f
32 .I progfile
33 ]
34 [
35 .I file ...
36 ]
37 .SH DESCRIPTION
38 .I Awk
39 scans each input
40 .I file
41 for lines that match any of a set of patterns specified literally in
42 .I prog
43 or in one or more files
44 specified as
45 .B \-f
46 .IR progfile .
47 With each pattern
48 there can be an associated action that will be performed
49 when a line of a
50 .I file
51 matches the pattern.
52 Each line is matched against the
53 pattern portion of every pattern-action statement;
54 the associated action is performed for each matched pattern.
55 The file name
56 .B \-
57 means the standard input.
58 Any
59 .I file
60 of the form
61 .I var=value
62 is treated as an assignment, not a filename,
63 and is executed at the time it would have been opened if it were a filename.
64 The option
65 .B \-v
66 followed by
67 .I var=value
68 is an assignment to be done before
69 .I prog
70 is executed;
71 any number of
72 .B \-v
73 options may be present.
74 The
75 .B \-F
76 .I fs
77 option defines the input field separator to be the regular expression
78 .IR fs .
79 .PP
80 An input line is normally made up of fields separated by white space,
81 or by the regular expression
82 .BR FS .
83 The fields are denoted
84 .BR $1 ,
85 .BR $2 ,
86 \&..., while
87 .B $0
88 refers to the entire line.
89 If
90 .BR FS
91 is null, the input line is split into one field per character.
92 .PP
93 A pattern-action statement has the form:
94 .IP
95 .IB pattern " { " action " }
96 .PP
97 A missing
98 .BI { " action " }
99 means print the line;
100 a missing pattern always matches.
101 Pattern-action statements are separated by newlines or semicolons.
102 .PP
103 An action is a sequence of statements.
104 A statement can be one of the following:
105 .PP
106 .EX
107 .ta \w'\f(CWdelete array[expression]\fR'u
108 .RS
109 .nf
110 .ft CW
111 if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP
112 while(\fI expression \fP)\fI statement\fP
113 for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP
114 for(\fI var \fPin\fI array \fP)\fI statement\fP
115 do\fI statement \fPwhile(\fI expression \fP)
116 break
117 continue
118 {\fR [\fP\fI statement ... \fP\fR] \fP}
119 \fIexpression\fP        #\fR commonly\fP\fI var = expression\fP
120 print\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
121 printf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
122 return\fR [ \fP\fIexpression \fP\fR]\fP
123 next    #\fR skip remaining patterns on this input line\fP
124 nextfile        #\fR skip rest of this file, open next, start at top\fP
125 delete\fI array\fP[\fI expression \fP]  #\fR delete an array element\fP
126 delete\fI array\fP      #\fR delete all elements of array\fP
127 exit\fR [ \fP\fIexpression \fP\fR]\fP   #\fR exit immediately; status is \fP\fIexpression\fP
128 .fi
129 .RE
130 .EE
131 .DT
132 .PP
133 Statements are terminated by
134 semicolons, newlines or right braces.
135 An empty
136 .I expression-list
137 stands for
138 .BR $0 .
139 String constants are quoted \&\f(CW"\ "\fR,
140 with the usual C escapes recognized within.
141 Expressions take on string or numeric values as appropriate,
142 and are built using the operators
143 .B + \- * / % ^
144 (exponentiation), and concatenation (indicated by white space).
145 The operators
146 .B
147 ! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:
148 are also available in expressions.
149 Variables may be scalars, array elements
150 (denoted
151 .IB x  [ i ] \fR)
152 or fields.
153 Variables are initialized to the null string.
154 Array subscripts may be any string,
155 not necessarily numeric;
156 this allows for a form of associative memory.
157 Multiple subscripts such as
158 .B [i,j,k]
159 are permitted; the constituents are concatenated,
160 separated by the value of
161 .BR SUBSEP .
162 .PP
163 The
164 .B print
165 statement prints its arguments on the standard output
166 (or on a file if
167 .BI > " file
168 or
169 .BI >> " file
170 is present or on a pipe if
171 .BI | " cmd
172 is present), separated by the current output field separator,
173 and terminated by the output record separator.
174 .I file
175 and
176 .I cmd
177 may be literal names or parenthesized expressions;
178 identical string values in different statements denote
179 the same open file.
180 The
181 .B printf
182 statement formats its expression list according to the
183 .I format
184 (see
185 .IR printf (3)).
186 The built-in function
187 .BI close( expr )
188 closes the file or pipe
189 .IR expr .
190 The built-in function
191 .BI fflush( expr )
192 flushes any buffered output for the file or pipe
193 .IR expr .
194 .PP
195 The mathematical functions
196 .BR atan2 ,
197 .BR cos ,
198 .BR exp ,
199 .BR log ,
200 .BR sin ,
201 and
202 .B sqrt
203 are built in.
204 Other built-in functions:
205 .TF length
206 .TP
207 .B length
208 the length of its argument
209 taken as a string,
210 number of elements in an array for an array argument,
211 or length of
212 .B $0
213 if no argument.
214 .TP
215 .B rand
216 random number on [0,1).
217 .TP
218 .B srand
219 sets seed for
220 .B rand
221 and returns the previous seed.
222 .TP
223 .B int
224 truncates to an integer value.
225 .TP
226 \fBsubstr(\fIs\fB, \fIm\fR [\fB, \fIn\^\fR]\fB)\fR
227 the
228 .IR n -character
229 substring of
230 .I s
231 that begins at position
232 .I m
233 counted from 1.
234 If no
235 .IR n ,
236 use the rest of the string.
237 .TP
238 .BI index( s , " t" )
239 the position in
240 .I s
241 where the string
242 .I t
243 occurs, or 0 if it does not.
244 .TP
245 .BI match( s , " r" )
246 the position in
247 .I s
248 where the regular expression
249 .I r
250 occurs, or 0 if it does not.
251 The variables
252 .B RSTART
253 and
254 .B RLENGTH
255 are set to the position and length of the matched string.
256 .TP
257 \fBsplit(\fIs\fB, \fIa \fR[\fB, \fIfs\^\fR]\fB)\fR
258 splits the string
259 .I s
260 into array elements
261 .IB a [1] \fR,
262 .IB a [2] \fR,
263 \&...,
264 .IB a [ n ] \fR,
265 and returns
266 .IR n .
267 The separation is done with the regular expression
268 .I fs
269 or with the field separator
270 .B FS
271 if
272 .I fs
273 is not given.
274 An empty string as field separator splits the string
275 into one array element per character.
276 .TP
277 \fBsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
278 substitutes
279 .I t
280 for the first occurrence of the regular expression
281 .I r
282 in the string
283 .IR s .
284 If
285 .I s
286 is not given,
287 .B $0
288 is used.
289 .TP
290 \fBgsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
291 same as
292 .B sub
293 except that all occurrences of the regular expression
294 are replaced;
295 .B sub
296 and
297 .B gsub
298 return the number of replacements.
299 .TP
300 .BI sprintf( fmt , " expr" , " ...\fB)
301 the string resulting from formatting
302 .I expr ...
303 according to the
304 .IR printf (3)
305 format
306 .IR fmt .
307 .TP
308 .BI system( cmd )
309 executes
310 .I cmd
311 and returns its exit status. This will be \-1 upon error,
312 .IR cmd 's
313 exit status upon a normal exit,
314 256 +
315 .I sig
316 upon death-by-signal, where
317 .I sig
318 is the number of the murdering signal,
319 or 512 +
320 .I sig
321 if there was a core dump.
322 .TP
323 .BI tolower( str )
324 returns a copy of
325 .I str
326 with all upper-case characters translated to their
327 corresponding lower-case equivalents.
328 .TP
329 .BI toupper( str )
330 returns a copy of
331 .I str
332 with all lower-case characters translated to their
333 corresponding upper-case equivalents.
334 .PD
335 .PP
336 The ``function''
337 .B getline
338 sets
339 .B $0
340 to the next input record from the current input file;
341 .B getline
342 .BI < " file
343 sets
344 .B $0
345 to the next record from
346 .IR file .
347 .B getline
348 .I x
349 sets variable
350 .I x
351 instead.
352 Finally,
353 .IB cmd " | getline
354 pipes the output of
355 .I cmd
356 into
357 .BR getline ;
358 each call of
359 .B getline
360 returns the next line of output from
361 .IR cmd .
362 In all cases,
363 .B getline
364 returns 1 for a successful input,
365 0 for end of file, and \-1 for an error.
366 .PP
367 Patterns are arbitrary Boolean combinations
368 (with
369 .BR "! || &&" )
370 of regular expressions and
371 relational expressions.
372 Regular expressions are as in
373 .IR egrep ;
374 see
375 .IR grep (1).
376 Isolated regular expressions
377 in a pattern apply to the entire line.
378 Regular expressions may also occur in
379 relational expressions, using the operators
380 .B ~
381 and
382 .BR !~ .
383 .BI / re /
384 is a constant regular expression;
385 any string (constant or variable) may be used
386 as a regular expression, except in the position of an isolated regular expression
387 in a pattern.
388 .PP
389 A pattern may consist of two patterns separated by a comma;
390 in this case, the action is performed for all lines
391 from an occurrence of the first pattern
392 though an occurrence of the second.
393 .PP
394 A relational expression is one of the following:
395 .IP
396 .I expression matchop regular-expression
397 .br
398 .I expression relop expression
399 .br
400 .IB expression " in " array-name
401 .br
402 .BI ( expr , expr,... ") in " array-name
403 .PP
404 where a
405 .I relop
406 is any of the six relational operators in C,
407 and a
408 .I matchop
409 is either
410 .B ~
411 (matches)
412 or
413 .B !~
414 (does not match).
415 A conditional is an arithmetic expression,
416 a relational expression,
417 or a Boolean combination
418 of these.
419 .PP
420 The special patterns
421 .B BEGIN
422 and
423 .B END
424 may be used to capture control before the first input line is read
425 and after the last.
426 .B BEGIN
427 and
428 .B END
429 do not combine with other patterns.
430 They may appear multiple times in a program and execute
431 in the order they are read by
432 .IR awk .
433 .PP
434 Variable names with special meanings:
435 .TF FILENAME
436 .TP
437 .B ARGC
438 argument count, assignable.
439 .TP
440 .B ARGV
441 argument array, assignable;
442 non-null members are taken as filenames.
443 .TP
444 .B CONVFMT
445 conversion format used when converting numbers
446 (default
447 .BR "%.6g" ).
448 .TP
449 .B ENVIRON
450 array of environment variables; subscripts are names.
451 .TP
452 .B FILENAME
453 the name of the current input file.
454 .TP
455 .B FNR
456 ordinal number of the current record in the current file.
457 .TP
458 .B FS
459 regular expression used to separate fields; also settable
460 by option
461 .BI \-F fs\fR.
462 .TP
463 .BR NF
464 number of fields in the current record.
465 .TP
466 .B NR
467 ordinal number of the current record.
468 .TP
469 .B OFMT
470 output format for numbers (default
471 .BR "%.6g" ).
472 .TP
473 .B OFS
474 output field separator (default space).
475 .TP
476 .B ORS
477 output record separator (default newline).
478 .TP
479 .B RLENGTH
480 the length of a string matched by
481 .BR match .
482 .TP
483 .B RS
484 input record separator (default newline).
485 If empty, blank lines separate records.
486 If more than one character long,
487 .B RS
488 is treated as a regular expression, and records are
489 separated by text matching the expression.
490 .TP
491 .B RSTART
492 the start position of a string matched by
493 .BR match .
494 .TP
495 .B SUBSEP
496 separates multiple subscripts (default 034).
497 .PD
498 .PP
499 Functions may be defined (at the position of a pattern-action statement) thus:
500 .IP
501 .B
502 function foo(a, b, c) { ...; return x }
503 .PP
504 Parameters are passed by value if scalar and by reference if array name;
505 functions may be called recursively.
506 Parameters are local to the function; all other variables are global.
507 Thus local variables may be created by providing excess parameters in
508 the function definition.
509 .SH ENVIRONMENT VARIABLES
510 If
511 .B POSIXLY_CORRECT
512 is set in the environment, then
513 .I awk
514 follows the POSIX rules for
515 .B sub
516 and
517 .B gsub
518 with respect to consecutive backslashes and ampersands.
519 .SH EXAMPLES
520 .TP
521 .EX
522 length($0) > 72
523 .EE
524 Print lines longer than 72 characters.
525 .TP
526 .EX
527 { print $2, $1 }
528 .EE
529 Print first two fields in opposite order.
530 .PP
531 .EX
532 BEGIN { FS = ",[ \et]*|[ \et]+" }
533       { print $2, $1 }
534 .EE
535 .ns
536 .IP
537 Same, with input fields separated by comma and/or spaces and tabs.
538 .PP
539 .EX
540 .nf
541         { s += $1 }
542 END     { print "sum is", s, " average is", s/NR }
543 .fi
544 .EE
545 .ns
546 .IP
547 Add up first column, print sum and average.
548 .TP
549 .EX
550 /start/, /stop/
551 .EE
552 Print all lines between start/stop pairs.
553 .PP
554 .EX
555 .nf
556 BEGIN   {       # Simulate echo(1)
557         for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
558         printf "\en"
559         exit }
560 .fi
561 .EE
562 .SH SEE ALSO
563 .IR grep (1),
564 .IR lex (1),
565 .IR sed (1)
566 .br
567 A. V. Aho, B. W. Kernighan, P. J. Weinberger,
568 .IR "The AWK Programming Language" ,
569 Addison-Wesley, 1988.  ISBN 0-201-07981-X.
570 .SH BUGS
571 There are no explicit conversions between numbers and strings.
572 To force an expression to be treated as a number add 0 to it;
573 to force it to be treated as a string concatenate
574 \&\f(CW""\fP to it.
575 .PP
576 The scope rules for variables in functions are a botch;
577 the syntax is worse.
578 .PP
579 Only eight-bit characters sets are handled correctly.
580 .SH UNUSUAL FLOATING-POINT VALUES
581 .I Awk
582 was designed before IEEE 754 arithmetic defined Not-A-Number (NaN)
583 and Infinity values, which are supported by all modern floating-point
584 hardware.
585 .PP
586 Because
587 .I awk
588 uses
589 .IR strtod (3)
590 and
591 .IR atof (3)
592 to convert string values to double-precision floating-point values,
593 modern C libraries also convert strings starting with
594 .B inf
595 and
596 .B nan
597 into infinity and NaN values respectively.  This led to strange results,
598 with something like this:
599 .PP
600 .EX
601 .nf
602 echo nancy | awk '{ print $1 + 0 }'
603 .fi
604 .EE
605 .PP
606 printing
607 .B nan
608 instead of zero.
609 .PP
610 .I Awk
611 now follows GNU AWK, and prefilters string values before attempting
612 to convert them to numbers, as follows:
613 .TP
614 .I "Hexadecimal values"
615 Hexadecimal values (allowed since C99) convert to zero, as they did
616 prior to C99.
617 .TP
618 .I "NaN values"
619 The two strings
620 .B +nan
621 and
622 .B \-nan
623 (case independent) convert to NaN. No others do.
624 (NaNs can have signs.)
625 .TP
626 .I "Infinity values"
627 The two strings
628 .B +inf
629 and
630 .B \-inf
631 (case independent) convert to positive and negative infinity, respectively.
632 No others do.