Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / lang / nawk / files / awk.1
blob24f2923e5de16e5030eba94fe44681034fe0d4e9
1 .\" $NetBSD$
2 .\"
3 .de EX
4 .nf
5 .ft CW
6 ..
7 .de EE
8 .br
9 .fi
10 .ft 1
12 awk
13 .TH AWK 1
14 .CT 1 files prog_other
15 .SH NAME
16 awk \- pattern-directed scanning and processing language
17 .SH SYNOPSIS
18 .B awk
20 .BI \-F
21 .I fs
24 .BI \-v
25 .I var=value
28 .I 'prog'
30 .BI \-f
31 .I progfile
34 .I file ...
36 .SH DESCRIPTION
37 .I Awk
38 scans each input
39 .I file
40 for lines that match any of a set of patterns specified literally in
41 .IR prog
42 or in one or more files
43 specified as
44 .B \-f
45 .IR progfile .
46 With each pattern
47 there can be an associated action that will be performed
48 when a line of a
49 .I file
50 matches the pattern.
51 Each line is matched against the
52 pattern portion of every pattern-action statement;
53 the associated action is performed for each matched pattern.
54 The file name 
55 .B \-
56 means the standard input.
57 Any
58 .IR file
59 of the form
60 .I var=value
61 is treated as an assignment, not a filename,
62 and is executed at the time it would have been opened if it were a filename.
63 The option
64 .B \-v
65 followed by
66 .I var=value
67 is an assignment to be done before
68 .I prog
69 is executed;
70 any number of
71 .B \-v
72 options may be present.
73 The
74 .B \-F
75 .IR fs
76 option defines the input field separator to be the regular expression
77 .IR fs.
78 .PP
79 An input line is normally made up of fields separated by white space,
80 or by regular expression
81 .BR FS .
82 The fields are denoted
83 .BR $1 ,
84 .BR $2 ,
85 \&..., while
86 .B $0
87 refers to the entire line.
89 .BR FS
90 is null, the input line is split into one field per character.
91 .PP
92 A pattern-action statement has the form
93 .IP
94 .IB pattern " { " action " }
95 .PP
96 A missing 
97 .BI { " action " }
98 means print the line;
99 a missing pattern always matches.
100 Pattern-action statements are separated by newlines or semicolons.
102 An action is a sequence of statements.
103 A statement can be one of the following:
106 .ta \w'\f(CWdelete array[expression]'u
109 .ft CW
110 if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP
111 while(\fI expression \fP)\fI statement\fP
112 for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP
113 for(\fI var \fPin\fI array \fP)\fI statement\fP
114 do\fI statement \fPwhile(\fI expression \fP)
115 break
116 continue
117 {\fR [\fP\fI statement ... \fP\fR] \fP}
118 \fIexpression\fP        #\fR commonly\fP\fI var = expression\fP
119 print\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
120 printf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
121 return\fR [ \fP\fIexpression \fP\fR]\fP
122 next    #\fR skip remaining patterns on this input line\fP
123 nextfile        #\fR skip rest of this file, open next, start at top\fP
124 delete\fI array\fP[\fI expression \fP]  #\fR delete an array element\fP
125 delete\fI array\fP      #\fR delete all elements of array\fP
126 exit\fR [ \fP\fIexpression \fP\fR]\fP   #\fR exit immediately; status is \fP\fIexpression\fP
132 Statements are terminated by
133 semicolons, newlines or right braces.
134 An empty
135 .I expression-list
136 stands for
137 .BR $0 .
138 String constants are quoted \&\f(CW"\ "\fR,
139 with the usual C escapes recognized within.
140 Expressions take on string or numeric values as appropriate,
141 and are built using the operators
142 .B + \- * / % ^
143 (exponentiation), and concatenation (indicated by white space).
144 The operators
146 ! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:
147 are also available in expressions.
148 Variables may be scalars, array elements
149 (denoted
150 .IB x  [ i ] )
151 or fields.
152 Variables are initialized to the null string.
153 Array subscripts may be any string,
154 not necessarily numeric;
155 this allows for a form of associative memory.
156 Multiple subscripts such as
157 .B [i,j,k]
158 are permitted; the constituents are concatenated,
159 separated by the value of
160 .BR SUBSEP .
163 .B print
164 statement prints its arguments on the standard output
165 (or on a file if
166 .BI > file
168 .BI >> file
169 is present or on a pipe if
170 .BI | cmd
171 is present), separated by the current output field separator,
172 and terminated by the output record separator.
173 .I file
175 .I cmd
176 may be literal names or parenthesized expressions;
177 identical string values in different statements denote
178 the same open file.
180 .B printf
181 statement formats its expression list according to the format
182 (see
183 .IR printf (3)) .
184 The built-in function
185 .BI close( expr )
186 closes the file or pipe
187 .IR expr .
188 The built-in function
189 .BI fflush( expr )
190 flushes any buffered output for the file or pipe
191 .IR expr .
193 The mathematical functions
194 .BR exp ,
195 .BR log ,
196 .BR sqrt ,
197 .BR sin ,
198 .BR cos ,
200 .BR atan2 
201 are built in.
202 Other built-in functions:
203 .TF length
205 .B length
206 the length of its argument
207 taken as a string,
208 or of
209 .B $0
210 if no argument.
212 .B rand
213 random number on (0,1)
215 .B srand
216 sets seed for
217 .B rand
218 and returns the previous seed.
220 .B int
221 truncates to an integer value
223 .BI substr( s , " m" , " n\fB)
225 .IR n -character
226 substring of
227 .I s
228 that begins at position
229 .IR m 
230 counted from 1.
232 .BI index( s , " t" )
233 the position in
234 .I s
235 where the string
236 .I t
237 occurs, or 0 if it does not.
239 .BI match( s , " r" )
240 the position in
241 .I s
242 where the regular expression
243 .I r
244 occurs, or 0 if it does not.
245 The variables
246 .B RSTART
248 .B RLENGTH
249 are set to the position and length of the matched string.
251 .BI split( s , " a" , " fs\fB)
252 splits the string
253 .I s
254 into array elements
255 .IB a [1] ,
256 .IB a [2] ,
257 \&...,
258 .IB a [ n ] ,
259 and returns
260 .IR n .
261 The separation is done with the regular expression
262 .I fs
263 or with the field separator
264 .B FS
266 .I fs
267 is not given.
268 An empty string as field separator splits the string
269 into one array element per character.
271 .BI sub( r , " t" , " s\fB)
272 substitutes
273 .I t
274 for the first occurrence of the regular expression
275 .I r
276 in the string
277 .IR s .
279 .I s
280 is not given,
281 .B $0
282 is used.
284 .B gsub
285 same as
286 .B sub
287 except that all occurrences of the regular expression
288 are replaced;
289 .B sub
291 .B gsub
292 return the number of replacements.
294 .BI sprintf( fmt , " expr" , " ...\fB )
295 the string resulting from formatting
296 .I expr ...
297 according to the
298 .IR printf (3)
299 format
300 .I fmt
302 .BI system( cmd )
303 executes
304 .I cmd
305 and returns its exit status
307 .BI tolower( str )
308 returns a copy of
309 .I str
310 with all upper-case characters translated to their
311 corresponding lower-case equivalents.
313 .BI toupper( str )
314 returns a copy of
315 .I str
316 with all lower-case characters translated to their
317 corresponding upper-case equivalents.
320 The ``function''
321 .B getline
322 sets
323 .B $0
324 to the next input record from the current input file;
325 .B getline
326 .BI < file
327 sets
328 .B $0
329 to the next record from
330 .IR file .
331 .B getline
332 .I x
333 sets variable
334 .I x
335 instead.
336 Finally,
337 .IB cmd " | getline
338 pipes the output of
339 .I cmd
340 into
341 .BR getline ;
342 each call of
343 .B getline
344 returns the next line of output from
345 .IR cmd .
346 In all cases,
347 .B getline
348 returns 1 for a successful input,
349 0 for end of file, and \-1 for an error.
351 Patterns are arbitrary Boolean combinations
352 (with
353 .BR "! || &&" )
354 of regular expressions and
355 relational expressions.
356 Regular expressions are as in
357 .IR egrep ; 
359 .IR grep (1).
360 Isolated regular expressions
361 in a pattern apply to the entire line.
362 Regular expressions may also occur in
363 relational expressions, using the operators
364 .BR ~
366 .BR !~ .
367 .BI / re /
368 is a constant regular expression;
369 any string (constant or variable) may be used
370 as a regular expression, except in the position of an isolated regular expression
371 in a pattern.
373 A pattern may consist of two patterns separated by a comma;
374 in this case, the action is performed for all lines
375 from an occurrence of the first pattern
376 though an occurrence of the second.
378 A relational expression is one of the following:
380 .I expression matchop regular-expression
382 .I expression relop expression
384 .IB expression " in " array-name
386 .BI ( expr , expr,... ") in " array-name
388 where a relop is any of the six relational operators in C,
389 and a matchop is either
390 .B ~
391 (matches)
393 .B !~
394 (does not match).
395 A conditional is an arithmetic expression,
396 a relational expression,
397 or a Boolean combination
398 of these.
400 The special patterns
401 .B BEGIN
403 .B END
404 may be used to capture control before the first input line is read
405 and after the last.
406 .B BEGIN
408 .B END
409 do not combine with other patterns.
411 Variable names with special meanings:
412 .TF FILENAME
414 .B CONVFMT
415 conversion format used when converting numbers
416 (default
417 .BR "%.6g" )
419 .B FS
420 regular expression used to separate fields; also settable
421 by option
422 .BI \-F fs.
424 .BR NF
425 number of fields in the current record
427 .B NR
428 ordinal number of the current record
430 .B FNR
431 ordinal number of the current record in the current file
433 .B FILENAME
434 the name of the current input file
436 .B RS
437 input record separator (default newline)
439 .B OFS
440 output field separator (default blank)
442 .B ORS
443 output record separator (default newline)
445 .B OFMT
446 output format for numbers (default
447 .BR "%.6g" )
449 .B SUBSEP
450 separates multiple subscripts (default 034)
452 .B ARGC
453 argument count, assignable
455 .B ARGV
456 argument array, assignable;
457 non-null members are taken as filenames
459 .B ENVIRON
460 array of environment variables; subscripts are names.
463 Functions may be defined (at the position of a pattern-action statement) thus:
466 function foo(a, b, c) { ...; return x }
468 Parameters are passed by value if scalar and by reference if array name;
469 functions may be called recursively.
470 Parameters are local to the function; all other variables are global.
471 Thus local variables may be created by providing excess parameters in
472 the function definition.
473 .SH EXAMPLES
476 length($0) > 72
478 Print lines longer than 72 characters.
481 { print $2, $1 }
483 Print first two fields in opposite order.
486 BEGIN { FS = ",[ \et]*|[ \et]+" }
487       { print $2, $1 }
491 Same, with input fields separated by comma and/or blanks and tabs.
495         { s += $1 }
496 END     { print "sum is", s, " average is", s/NR }
501 Add up first column, print sum and average.
504 /start/, /stop/
506 Print all lines between start/stop pairs.
510 BEGIN   {       # Simulate echo(1)
511         for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
512         printf "\en"
513         exit }
516 .SH SEE ALSO
517 .IR lex (1), 
518 .IR sed (1)
520 A. V. Aho, B. W. Kernighan, P. J. Weinberger,
522 The AWK Programming Language,
523 Addison-Wesley, 1988.  ISBN 0-201-07981-X
524 .SH BUGS
525 There are no explicit conversions between numbers and strings.
526 To force an expression to be treated as a number add 0 to it;
527 to force it to be treated as a string concatenate
528 \&\f(CW""\fP to it.
530 The scope rules for variables in functions are a botch;
531 the syntax is worse.