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.
234 .BI index( s , " t" )
239 occurs, or 0 if it does not.
241 .BI match( s , " r" )
244 where the regular expression
246 occurs, or 0 if it does not.
251 are set to the position and length of the matched string.
253 \fBsplit(\fIs\fB, \fIa \fR[\fB, \fIfs\^\fR]\fB)\fR
263 The separation is done with the regular expression
265 or with the field separator
270 An empty string as field separator splits the string
271 into one array element per character.
273 \fBsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
276 for the first occurrence of the regular expression
286 \fBgsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB)
289 except that all occurrences of the regular expression
294 return the number of replacements.
296 .BI sprintf( fmt , " expr" , " ...\fB)
297 the string resulting from formatting
307 and returns its exit status. This will be \-1 upon error,
309 exit status upon a normal exit,
312 upon death-by-signal, where
314 is the number of the murdering signal,
317 if there was a core dump.
322 with all upper-case characters translated to their
323 corresponding lower-case equivalents.
328 with all lower-case characters translated to their
329 corresponding upper-case equivalents.
336 to the next input record from the current input file;
341 to the next record from
356 returns the next line of output from
360 returns 1 for a successful input,
361 0 for end of file, and \-1 for an error.
363 Patterns are arbitrary Boolean combinations
366 of regular expressions and
367 relational expressions.
368 Regular expressions are as in
372 Isolated regular expressions
373 in a pattern apply to the entire line.
374 Regular expressions may also occur in
375 relational expressions, using the operators
380 is a constant regular expression;
381 any string (constant or variable) may be used
382 as a regular expression, except in the position of an isolated regular expression
385 A pattern may consist of two patterns separated by a comma;
386 in this case, the action is performed for all lines
387 from an occurrence of the first pattern
388 though an occurrence of the second.
390 A relational expression is one of the following:
392 .I expression matchop regular-expression
394 .I expression relop expression
396 .IB expression " in " array-name
398 .BI ( expr , expr,... ") in " array-name
402 is any of the six relational operators in C,
411 A conditional is an arithmetic expression,
412 a relational expression,
413 or a Boolean combination
420 may be used to capture control before the first input line is read
425 do not combine with other patterns.
426 They may appear multiple times in a program and execute
427 in the order they are read by
430 Variable names with special meanings:
434 argument count, assignable.
437 argument array, assignable;
438 non-null members are taken as filenames.
441 conversion format used when converting numbers
446 array of environment variables; subscripts are names.
449 the name of the current input file.
452 ordinal number of the current record in the current file.
455 regular expression used to separate fields; also settable
460 number of fields in the current record.
463 ordinal number of the current record.
466 output format for numbers (default
470 output field separator (default space).
473 output record separator (default newline).
476 the length of a string matched by
480 input record separator (default newline).
481 If empty, blank lines separate records.
482 If more than one character long,
484 is treated as a regular expression, and records are
485 separated by text matching the expression.
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.
505 .SH ENVIRONMENT VARIABLES
508 is set in the environment, then
510 follows the POSIX rules for
514 with respect to consecutive backslashes and ampersands.
520 Print lines longer than 72 characters.
525 Print first two fields in opposite order.
528 BEGIN { FS = ",[ \et]*|[ \et]+" }
533 Same, with input fields separated by comma and/or spaces and tabs.
538 END { print "sum is", s, " average is", s/NR }
543 Add up first column, print sum and average.
548 Print all lines between start/stop pairs.
552 BEGIN { # Simulate echo(1)
553 for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
563 A. V. Aho, B. W. Kernighan, P. J. Weinberger,
564 .IR "The AWK Programming Language" ,
565 Addison-Wesley, 1988. ISBN 0-201-07981-X.
567 There are no explicit conversions between numbers and strings.
568 To force an expression to be treated as a number add 0 to it;
569 to force it to be treated as a string concatenate
572 The scope rules for variables in functions are a botch;
575 Only eight-bit characters sets are handled correctly.