11 .CT 1 files prog_other
13 awk \- pattern-directed scanning and processing language
37 for lines that match any of a set of patterns specified literally in
39 or in one or more files
44 there can be an associated action that will be performed
48 Each line is matched against the
49 pattern portion of every pattern-action statement;
50 the associated action is performed for each matched pattern.
53 means the standard input.
58 is treated as an assignment, not a filename,
59 and is executed at the time it would have been opened if it were a filename.
64 is an assignment to be done before
69 options may be present.
73 option defines the input field separator to be the regular expression
76 An input line is normally made up of fields separated by white space,
77 or by the regular expression
79 The fields are denoted
84 refers to the entire line.
87 is null, the input line is split into one field per character.
89 A pattern-action statement has the form:
91 .IB pattern " { " action " }
96 a missing pattern always matches.
97 Pattern-action statements are separated by newlines or semicolons.
99 An action is a sequence of statements.
100 A statement can be one of the following:
103 .ta \w'\f(CWdelete array[expression]\fR'u
107 if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP
108 while(\fI expression \fP)\fI statement\fP
109 for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP
110 for(\fI var \fPin\fI array \fP)\fI statement\fP
111 do\fI statement \fPwhile(\fI expression \fP)
114 {\fR [\fP\fI statement ... \fP\fR] \fP}
115 \fIexpression\fP #\fR commonly\fP\fI var = expression\fP
116 print\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
117 printf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP
118 return\fR [ \fP\fIexpression \fP\fR]\fP
119 next #\fR skip remaining patterns on this input line\fP
120 nextfile #\fR skip rest of this file, open next, start at top\fP
121 delete\fI array\fP[\fI expression \fP] #\fR delete an array element\fP
122 delete\fI array\fP #\fR delete all elements of array\fP
123 exit\fR [ \fP\fIexpression \fP\fR]\fP #\fR exit immediately; status is \fP\fIexpression\fP
129 Statements are terminated by
130 semicolons, newlines or right braces.
135 String constants are quoted \&\f(CW"\ "\fR,
136 with the usual C escapes recognized within.
137 Expressions take on string or numeric values as appropriate,
138 and are built using the operators
140 (exponentiation), and concatenation (indicated by white space).
143 ! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:
144 are also available in expressions.
145 Variables may be scalars, array elements
149 Variables are initialized to the null string.
150 Array subscripts may be any string,
151 not necessarily numeric;
152 this allows for a form of associative memory.
153 Multiple subscripts such as
155 are permitted; the constituents are concatenated,
156 separated by the value of
161 statement prints its arguments on the standard output
166 is present or on a pipe if
168 is present), separated by the current output field separator,
169 and terminated by the output record separator.
173 may be literal names or parenthesized expressions;
174 identical string values in different statements denote
178 statement formats its expression list according to the
182 The built-in function
184 closes the file or pipe
186 The built-in function
188 flushes any buffered output for the file or pipe
191 The mathematical functions
200 Other built-in functions:
204 the length of its argument
206 number of elements in an array for an array argument,
212 random number on [0,1)
217 and returns the previous seed.
220 truncates to an integer value
222 \fBsubstr(\fIs\fB, \fIm\fR [\fB, \fIn\^\fR]\fB)\fR
227 that begins at position
232 use the rest of the string
235 .BI index( s , " t" )
240 occurs, or 0 if it does not.
242 .BI match( s , " r" )
245 where the regular expression
247 occurs, or 0 if it does not.
252 are set to the position and length of the matched string.
254 \fBsplit(\fIs\fB, \fIa \fR[\fB, \fIfs\^\fR]\fB)\fR
264 The separation is done with the regular expression
266 or with the field separator
271 An empty string as field separator splits the string
272 into one array element per character.
274 \fBsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
277 for the first occurrence of the regular expression
287 \fBgsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
290 except that all occurrences of the regular expression
295 return the number of replacements.
297 .BI sprintf( fmt , " expr" , " ...\fB)
298 the string resulting from formatting
308 and returns its exit status. This will be \-1 upon error,
310 exit status upon a normal exit,
313 upon death-by-signal, where
315 is the number of the murdering signal,
318 if there was a core dump.
323 with all upper-case characters translated to their
324 corresponding lower-case equivalents.
329 with all lower-case characters translated to their
330 corresponding upper-case equivalents.
337 to the next input record from the current input file;
342 to the next record from
357 returns the next line of output from
361 returns 1 for a successful input,
362 0 for end of file, and \-1 for an error.
364 Patterns are arbitrary Boolean combinations
367 of regular expressions and
368 relational expressions.
369 Regular expressions are as in
371 except numeric quantifiers are not supported beyond the basic
375 for quantities \&\f(CW"0 or 1"\fP and \&\f(CW"1 or more"\fP
377 Isolated regular expressions
378 in a pattern apply to the entire line.
379 Regular expressions may also occur in
380 relational expressions, using the operators
385 is a constant regular expression;
386 any string (constant or variable) may be used
387 as a regular expression, except in the position of an isolated regular expression
390 A pattern may consist of two patterns separated by a comma;
391 in this case, the action is performed for all lines
392 from an occurrence of the first pattern
393 though an occurrence of the second.
395 A relational expression is one of the following:
397 .I expression matchop regular-expression
399 .I expression relop expression
401 .IB expression " in " array-name
403 .BI ( expr , expr,... ") in " array-name
407 is any of the six relational operators in C,
416 A conditional is an arithmetic expression,
417 a relational expression,
418 or a Boolean combination
425 may be used to capture control before the first input line is read
430 do not combine with other patterns.
431 They may appear multiple times in a program and execute
432 in the order they are read by
435 Variable names with special meanings:
439 argument count, assignable.
442 argument array, assignable;
443 non-null members are taken as filenames.
446 conversion format used when converting numbers
451 array of environment variables; subscripts are names.
454 the name of the current input file.
457 ordinal number of the current record in the current file.
460 regular expression used to separate fields; also settable
465 number of fields in the current record.
468 ordinal number of the current record.
471 output format for numbers (default
475 output field separator (default space).
478 output record separator (default newline).
481 the length of a string matched by
485 input record separator (default newline).
488 the start position of a string matched by
492 separates multiple subscripts (default 034).
495 Functions may be defined (at the position of a pattern-action statement) thus:
498 function foo(a, b, c) { ...; return x }
500 Parameters are passed by value if scalar and by reference if array name;
501 functions may be called recursively.
502 Parameters are local to the function; all other variables are global.
503 Thus local variables may be created by providing excess parameters in
504 the function definition.
510 Print lines longer than 72 characters.
515 Print first two fields in opposite order.
518 BEGIN { FS = ",[ \et]*|[ \et]+" }
523 Same, with input fields separated by comma and/or spaces and tabs.
528 END { print "sum is", s, " average is", s/NR }
533 Add up first column, print sum and average.
538 Print all lines between start/stop pairs.
542 BEGIN { # Simulate echo(1)
543 for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
553 A. V. Aho, B. W. Kernighan, P. J. Weinberger,
554 .IR "The AWK Programming Language" ,
555 Addison-Wesley, 1988. ISBN 0-201-07981-X.
557 There are no explicit conversions between numbers and strings.
558 To force an expression to be treated as a number add 0 to it;
559 to force it to be treated as a string concatenate
562 The scope rules for variables in functions are a botch;
565 Only eight-bit characters sets are handled correctly.