14 .CT 1 files prog_other
16 awk \- pattern-directed scanning and processing language
40 for lines that match any of a set of patterns specified literally in
42 or in one or more files
47 there can be an associated action that will be performed
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.
56 means the standard input.
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.
67 is an assignment to be done before
72 options may be present.
76 option defines the input field separator to be the regular expression
79 An input line is normally made up of fields separated by white space,
80 or by regular expression
82 The fields are denoted
87 refers to the entire line.
90 is null, the input line is split into one field per character.
92 A pattern-action statement has the form
94 .IB pattern " { " action " }
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
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)
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.
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
143 (exponentiation), and concatenation (indicated by white space).
146 ! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:
147 are also available in expressions.
148 Variables may be scalars, array elements
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
158 are permitted; the constituents are concatenated,
159 separated by the value of
164 statement prints its arguments on the standard output
169 is present or on a pipe if
171 is present), separated by the current output field separator,
172 and terminated by the output record separator.
176 may be literal names or parenthesized expressions;
177 identical string values in different statements denote
181 statement formats its expression list according to the format
184 The built-in function
186 closes the file or pipe
188 The built-in function
190 flushes any buffered output for the file or pipe
193 The mathematical functions
202 Other built-in functions:
206 the length of its argument
213 random number on (0,1)
218 and returns the previous seed.
221 truncates to an integer value
223 .BI substr( s , " m" , " n\fB)
228 that begins at position
232 .BI index( s , " t" )
237 occurs, or 0 if it does not.
239 .BI match( s , " r" )
242 where the regular expression
244 occurs, or 0 if it does not.
249 are set to the position and length of the matched string.
251 .BI split( s , " a" , " fs\fB)
261 The separation is done with the regular expression
263 or with the field separator
268 An empty string as field separator splits the string
269 into one array element per character.
271 .BI sub( r , " t" , " s\fB)
274 for the first occurrence of the regular expression
287 except that all occurrences of the regular expression
292 return the number of replacements.
294 .BI sprintf( fmt , " expr" , " ...\fB )
295 the string resulting from formatting
305 and returns its exit status
310 with all upper-case characters translated to their
311 corresponding lower-case equivalents.
316 with all lower-case characters translated to their
317 corresponding upper-case equivalents.
324 to the next input record from the current input file;
329 to the next record from
344 returns the next line of output from
348 returns 1 for a successful input,
349 0 for end of file, and \-1 for an error.
351 Patterns are arbitrary Boolean combinations
354 of regular expressions and
355 relational expressions.
356 Regular expressions are as in
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
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
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
395 A conditional is an arithmetic expression,
396 a relational expression,
397 or a Boolean combination
404 may be used to capture control before the first input line is read
409 do not combine with other patterns.
411 Variable names with special meanings:
415 conversion format used when converting numbers
420 regular expression used to separate fields; also settable
425 number of fields in the current record
428 ordinal number of the current record
431 ordinal number of the current record in the current file
434 the name of the current input file
437 input record separator (default newline)
440 output field separator (default blank)
443 output record separator (default newline)
446 output format for numbers (default
450 separates multiple subscripts (default 034)
453 argument count, assignable
456 argument array, assignable;
457 non-null members are taken as filenames
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.
478 Print lines longer than 72 characters.
483 Print first two fields in opposite order.
486 BEGIN { FS = ",[ \et]*|[ \et]+" }
491 Same, with input fields separated by comma and/or blanks and tabs.
496 END { print "sum is", s, " average is", s/NR }
501 Add up first column, print sum and average.
506 Print all lines between start/stop pairs.
510 BEGIN { # Simulate echo(1)
511 for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
520 A. V. Aho, B. W. Kernighan, P. J. Weinberger,
522 The AWK Programming Language,
523 Addison-Wesley, 1988. ISBN 0-201-07981-X
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
530 The scope rules for variables in functions are a botch;