]> git.lizzy.rs Git - plan9front.git/blob - sys/man/1/awk
walk(1): improve wording, formatting
[plan9front.git] / sys / man / 1 / awk
1 .TH AWK 1
2 .SH NAME
3 awk \- pattern-directed scanning and processing language
4 .SH SYNOPSIS
5 .B awk
6 [
7 .B -F
8 .I fs
9 ]
10 [
11 .B -d
12 ]
13 [
14 .BI -mf
15 .I n
16 ]
17 [
18 .B -mr
19 .I n
20 ]
21 [
22 .B -safe
23 ]
24 [
25 .B -v
26 .I var=value
27 ]
28 [
29 .B -f
30 .I progfile
31 |
32 .I prog
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 .L -
57 means the standard input.
58 Any
59 .IR file
60 of the form
61 .I var=value
62 is treated as an assignment, not a file name,
63 and is executed at the time it would have been opened if it were a file name.
64 The option
65 .B -v
66 followed by
67 .I var=value
68 is an assignment to be done before the program
69 is executed;
70 any number of
71 .B -v
72 options may be present.
73 .B -F
74 .IR fs
75 option defines the input field separator to be the regular expression
76 .IR fs .
77 .PP
78 An input line is normally made up of fields separated by white space,
79 or by regular expression
80 .BR FS .
81 The fields are denoted
82 .BR $1 ,
83 .BR $2 ,
84 \&..., while
85 .B $0
86 refers to the entire line.
87 If
88 .BR FS
89 is null, the input line is split into one field per character.
90 .PP
91 To compensate for inadequate implementation of storage management,
92 the 
93 .B -mr
94 option can be used to set the maximum size of the input record,
95 and the
96 .B -mf
97 option to set the maximum number of fields.
98 .PP
99 The
100 .B -safe
101 option causes
102 .I awk
103 to run in 
104 ``safe mode,''
105 in which it is not allowed to 
106 run shell commands or open files
107 and the environment is not made available
108 in the 
109 .B ENVIRON
110 variable.
111 .PP
112 A pattern-action statement has the form
113 .IP
114 .IB pattern " { " action " }
115 .PP
116 A missing 
117 .BI { " action " }
118 means print the line;
119 a missing pattern always matches.
120 Pattern-action statements are separated by newlines or semicolons.
121 .PP
122 An action is a sequence of statements.
123 A statement can be one of the following:
124 .PP
125 .EX
126 .ta \w'\fLdelete array[expression]'u
127 if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP
128 while(\fI expression \fP)\fI statement\fP
129 for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP
130 for(\fI var \fPin\fI array \fP)\fI statement\fP
131 do\fI statement \fPwhile(\fI expression \fP)
132 break
133 continue
134 {\fR [\fP\fI statement ... \fP\fR] \fP}
135 \fIexpression\fP        #\fR commonly\fP\fI var = expression\fP
136 print\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
137 printf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
138 return\fR [ \fP\fIexpression \fP\fR]\fP
139 next    #\fR skip remaining patterns on this input line\fP
140 nextfile        #\fR skip rest of this file, open next, start at top\fP
141 delete\fI array\fP[\fI expression \fP]  #\fR delete an array element\fP
142 delete\fI array\fP      #\fR delete all elements of array\fP
143 exit\fR [ \fP\fIexpression \fP\fR]\fP   #\fR exit immediately; status is \fP\fIexpression\fP
144 .EE
145 .DT
146 .PP
147 Statements are terminated by
148 semicolons, newlines or right braces.
149 An empty
150 .I expression-list
151 stands for
152 .BR $0 .
153 String constants are quoted \&\fL"\ "\fR,
154 with the usual C escapes recognized within.
155 Expressions take on string or numeric values as appropriate,
156 and are built using the operators
157 .B + \- * / % ^
158 (exponentiation), and concatenation (indicated by white space).
159 The operators
160 .B
161 ! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:
162 are also available in expressions.
163 Variables may be scalars, array elements
164 (denoted
165 .IB x  [ i ] )
166 or fields.
167 Variables are initialized to the null string.
168 Array subscripts may be any string,
169 not necessarily numeric;
170 this allows for a form of associative memory.
171 Multiple subscripts such as
172 .B [i,j,k]
173 are permitted; the constituents are concatenated,
174 separated by the value of
175 .BR SUBSEP .
176 .PP
177 The
178 .B print
179 statement prints its arguments on the standard output
180 (or on a file if
181 .BI > file
182 or
183 .BI >> file
184 is present or on a pipe if
185 .BI | cmd
186 is present), separated by the current output field separator,
187 and terminated by the output record separator.
188 .I file
189 and
190 .I cmd
191 may be literal names or parenthesized expressions;
192 identical string values in different statements denote
193 the same open file.
194 The
195 .B printf
196 statement formats its expression list according to the format
197 (see
198 .IR fprintf (2)) .
199 The built-in function
200 .BI close( expr )
201 closes the file or pipe
202 .IR expr .
203 The built-in function
204 .BI fflush( expr )
205 flushes any buffered output for the file or pipe
206 .IR expr .
207 If
208 .IR expr
209 is omitted or is a null string, all open files are flushed.
210 .PP
211 The mathematical functions
212 .BR exp ,
213 .BR log ,
214 .BR sqrt ,
215 .BR sin ,
216 .BR cos ,
217 and
218 .BR atan2 
219 are built in.
220 Other built-in functions:
221 .TF length
222 .TP
223 .B length
224 If its argument is a string, the string's length is returned.
225 If its argument is an array, the number of subscripts in the array is returned.
226 If no argument, the length of
227 .B $0
228 is returned.
229 .TP
230 .B rand
231 random number on (0,1)
232 .TP
233 .B srand
234 sets seed for
235 .B rand
236 and returns the previous seed.
237 .TP
238 .B int
239 truncates to an integer value
240 .TP
241 .B utf
242 converts its numerical argument, a character number, to a
243 .SM UTF
244 string
245 .TP
246 .BI substr( s , " m")
247 the maximum length substring of
248 .I s
249 that begins at position
250 .IR m
251 counted from 1.
252 .TP
253 .BI substr( s , " m" , " n\fL)
254 the
255 .IR n -character
256 substring of
257 .I s
258 that begins at position
259 .IR m 
260 counted from 1.
261 .TP
262 .BI index( s , " t" )
263 the position in
264 .I s
265 where the string
266 .I t
267 occurs, or 0 if it does not.
268 .TP
269 .BI match( s , " r" )
270 the position in
271 .I s
272 where the regular expression
273 .I r
274 occurs, or 0 if it does not.
275 The variables
276 .B RSTART
277 and
278 .B RLENGTH
279 are set to the position and length of the matched string.
280 .TP
281 .BI split( s , " a" , " fs\fL)
282 splits the string
283 .I s
284 into array elements
285 .IB a [1]\f1,
286 .IB a [2]\f1,
287 \&...,
288 .IB a [ n ]\f1,
289 and returns
290 .IR n .
291 The separation is done with the regular expression
292 .I fs
293 or with the field separator
294 .B FS
295 if
296 .I fs
297 is not given.
298 An empty string as field separator splits the string
299 into one array element per character.
300 .TP
301 .BI sub( r , " t" , " s\fL)
302 substitutes
303 .I t
304 for the first occurrence of the regular expression
305 .I r
306 in the string
307 .IR s .
308 If
309 .I s
310 is not given,
311 .B $0
312 is used.
313 .B &
314 in
315 .I t
316 is replaced by the match.
317 .TP
318 .B gsub
319 same as
320 .B sub
321 except that all occurrences of the regular expression
322 are replaced;
323 .B sub
324 and
325 .B gsub
326 return the number of replacements.
327 .TP
328 .BI sprintf( fmt , " expr" , " ...\fL)
329 the string resulting from formatting
330 .I expr ...
331 according to the
332 .I printf
333 format
334 .I fmt
335 .TP
336 .BI system( cmd )
337 executes
338 .I cmd
339 and returns its exit status
340 .TP
341 .BI tolower( str )
342 returns a copy of
343 .I str
344 with all upper-case characters translated to their
345 corresponding lower-case equivalents.
346 .TP
347 .BI toupper( str )
348 returns a copy of
349 .I str
350 with all lower-case characters translated to their
351 corresponding upper-case equivalents.
352 .PD
353 .PP
354 The ``function''
355 .B getline
356 sets
357 .B $0
358 to the next input record from the current input file;
359 .B getline
360 .BI < file
361 sets
362 .B $0
363 to the next record from
364 .IR file .
365 .B getline
366 .I x
367 sets variable
368 .I x
369 instead.
370 Finally,
371 .IB cmd " | getline
372 pipes the output of
373 .I cmd
374 into
375 .BR getline ;
376 each call of
377 .B getline
378 returns the next line of output from
379 .IR cmd .
380 In all cases,
381 .B getline
382 returns 1 for a successful input,
383 0 for end of file, and \-1 for an error.
384 .PP
385 Patterns are arbitrary Boolean combinations
386 (with
387 .BR "! || &&" )
388 of regular expressions and
389 relational expressions.
390 Regular expressions are as in
391 .IR regexp (6).
392 Isolated regular expressions
393 in a pattern apply to the entire line.
394 Regular expressions may also occur in
395 relational expressions, using the operators
396 .BR ~
397 and
398 .BR !~ .
399 .BI / re /
400 is a constant regular expression;
401 any string (constant or variable) may be used
402 as a regular expression, except in the position of an isolated regular expression
403 in a pattern.
404 .PP
405 A pattern may consist of two patterns separated by a comma;
406 in this case, the action is performed for all lines
407 from an occurrence of the first pattern
408 though an occurrence of the second.
409 .PP
410 A relational expression is one of the following:
411 .IP
412 .I expression matchop regular-expression
413 .br
414 .I expression relop expression
415 .br
416 .IB expression " in " array-name
417 .br
418 .BI ( expr , expr,... ") in " array-name
419 .PP
420 where a
421 .I relop
422 is any of the six relational operators in C,
423 and a
424 .I matchop
425 is either
426 .B ~
427 (matches)
428 or
429 .B !~
430 (does not match).
431 A conditional is an arithmetic expression,
432 a relational expression,
433 or a Boolean combination
434 of these.
435 .PP
436 The special patterns
437 .B BEGIN
438 and
439 .B END
440 may be used to capture control before the first input line is read
441 and after the last.
442 .B BEGIN
443 and
444 .B END
445 do not combine with other patterns.
446 .PP
447 Variable names with special meanings:
448 .TF FILENAME
449 .TP
450 .B CONVFMT
451 conversion format used when converting numbers
452 (default
453 .BR "%.6g" )
454 .TP
455 .B FS
456 regular expression used to separate fields; also settable
457 by option
458 .BI \-F fs\f1.
459 .TP
460 .BR NF
461 number of fields in the current record
462 .TP
463 .B NR
464 ordinal number of the current record
465 .TP
466 .B FNR
467 ordinal number of the current record in the current file
468 .TP
469 .B FILENAME
470 the name of the current input file
471 .TP
472 .B RS
473 input record separator (default newline)
474 .TP
475 .B OFS
476 output field separator (default blank)
477 .TP
478 .B ORS
479 output record separator (default newline)
480 .TP
481 .B OFMT
482 output format for numbers (default
483 .BR "%.6g" )
484 .TP
485 .B SUBSEP
486 separates multiple subscripts (default 034)
487 .TP
488 .B ARGC
489 argument count, assignable
490 .TP
491 .B ARGV
492 argument array, assignable;
493 non-null members are taken as file names
494 .TP
495 .B ENVIRON
496 array of environment variables; subscripts are names.
497 .PD
498 .PP
499 Functions may be defined (at the position of a pattern-action statement) thus:
500 .IP
501 .L
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 EXAMPLES
510 .TP
511 .L
512 length($0) > 72
513 Print lines longer than 72 characters.
514 .TP
515 .L
516 { print $2, $1 }
517 Print first two fields in opposite order.
518 .PP
519 .EX
520 BEGIN { FS = ",[ \et]*|[ \et]+" }
521       { print $2, $1 }
522 .EE
523 .ns
524 .IP
525 Same, with input fields separated by comma and/or blanks and tabs.
526 .PP
527 .EX
528         { s += $1 }
529 END     { print "sum is", s, " average is", s/NR }
530 .EE
531 .ns
532 .IP
533 Add up first column, print sum and average.
534 .TP
535 .L
536 /start/, /stop/
537 Print all lines between start/stop pairs.
538 .PP
539 .EX
540 BEGIN   {       # Simulate echo(1)
541         for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
542         printf "\en"
543         exit }
544 .EE
545 .SH SOURCE
546 .B /sys/src/cmd/awk
547 .SH SEE ALSO
548 .IR sed (1),
549 .IR regexp (6),
550 .br
551 A. V. Aho, B. W. Kernighan, P. J. Weinberger,
552 .I
553 The AWK Programming Language,
554 Addison-Wesley, 1988.  ISBN 0-201-07981-X
555 .SH BUGS
556 There are no explicit conversions between numbers and strings.
557 To force an expression to be treated as a number add 0 to it;
558 to force it to be treated as a string concatenate
559 \&\fL""\fP to it.
560 .br
561 The scope rules for variables in functions are a botch;
562 the syntax is worse.
563 .br
564 UTF is not always dealt with correctly,
565 though
566 .I awk
567 does make an attempt to do so.
568 The
569 .I split
570 function with an empty string as final argument now copes
571 with UTF in the string being split.