3 .CD "awk \(en pattern matching language"
4 .SX "awk \fIrules\fR [\fIfile\fR] ...
6 .EX "awk rules input" "Process \fIinput\fR according to \fIrules\fR"
7 .EX "awk rules \(en >out" "Input from terminal, output to \fIout\fR"
9 AWK is a programming language devised by Aho, Weinberger, and Kernighan
10 at Bell Labs (hence the name).
11 \fIAwk\fR programs search files for
12 specific patterns and performs \*(OQactions\*(CQ for every occurrence
13 of these patterns. The patterns can be \*(OQregular expressions\*(CQ
14 as used in the \fIed\fR editor. The actions are expressed
15 using a subset of the C language.
17 The patterns and actions are usually placed in a \*(OQrules\*(CQ file
18 whose name must be the first argument in the command line,
19 preceded by the flag \fB\(enf\fR. Otherwise, the first argument on the
20 command line is taken to be a string containing the rules
21 themselves. All other arguments are taken to be the names of text
22 files on which the rules are to be applied, with \fB\(en\fR being the
23 standard input. To take rules from the standard input, use \fB\(enf \(en\fR.
27 .Cx "awk rules prog.\d\s+2*\s0\u"
29 would read the patterns and actions rules from the file \fIrules\fR
30 and apply them to all the arguments.
32 The general format of a rules file is:
34 ~~~<pattern> { <action> }
35 ~~~<pattern> { <action> }
38 There may be any number of these <pattern> { <action> }
39 sequences in the rules file. \fIAwk\fR reads a line of input from
40 the current input file and applies every <pattern> { <action> }
41 in sequence to the line.
43 If the <pattern> corresponding to any { <action> } is missing,
44 the action is applied to every line of input. The default
45 { <action> } is to print the matched input line.
48 The <pattern>s may consist of any valid C expression. If the
49 <pattern> consists of two expressions separated by a comma, it
50 is taken to be a range and the <action> is performed on all
51 lines of input that match the range. <pattern>s may contain
52 \*(OQregular expressions\*(CQ delimited by an @ symbol. Regular
53 expressions can be thought of as a generalized \*(OQwildcard\*(CQ
54 string matching mechanism, similar to that used by many
55 operating systems to specify file names. Regular expressions
56 may contain any of the following characters:
61 x An ordinary character
63 \\ The backslash quotes any character
65 ^ A circumflex at the beginning of an expr matches the beginning of a line.
67 $ A dollar-sign at the end of an expression matches the end of a line.
69 \&. A period matches any single character except newline.
71 * An expression followed by an asterisk matches zero or more occurrences
72 of that expression: \*(OQfo*\*(CQ matches \*(OQf\*(CQ, \*(OQfo\*(CQ, \*(OQfoo\*(CQ, \*(OQfooo\*(CQ, etc.
74 + An expression followed by a plus sign matches one or more occurrences
75 of that expression: \*(OQfo+\*(CQ matches \*(OQfo\*(CQ, \*(OQfoo\*(CQ, \*(OQfooo\*(CQ, etc.
77 [] A string enclosed in square brackets matches any single character in that
78 string, but no others. If the first character in the string is a circumflex, the
79 expression matches any character except newline and the characters in the
80 string. For example, \*(OQ[xyz]\*(CQ matches \*(OQxx\*(CQ and \*(OQzyx\*(CQ, while
81 \*(OQ[^xyz]\*(CQ matches \*(OQabc\*(CQ but not \*(OQaxb\*(CQ. A range of characters may be
82 specified by two characters separated by \*(OQ-\*(CQ.
86 Actions are expressed as a subset of the C language. All
87 variables are global and default to int's if not formally
89 Only char's and int's and pointers and arrays of
90 char and int are allowed. \fIAwk\fR allows only decimal integer
91 constants to be used\(emno hex (0xnn) or octal (0nn). String
92 and character constants may contain all of the special C
93 escapes (\\n, \\r, etc.).
95 \fIAwk\fR supports the \*(OQif\*(CQ, \*(OQelse\*(CQ,
96 \*(OQwhile\*(CQ and \*(OQbreak\*(CQ flow of
97 control constructs, which behave exactly as in C.
99 Also supported are the following unary and binary operators,
100 listed in order from highest to lowest precedence:
104 \fB Operator Type Associativity\fR
105 () [] unary left to right
107 ! ~ ++ \(en\(en \(en * & unary right to left
109 * / % binary left to right
110 + \(en binary left to right
111 << >> binary left to right
112 < <= > >= binary left to right
113 == != binary left to right
114 & binary left to right
115 ^ binary left to right
116 | binary left to right
117 && binary left to right
118 || binary left to right
119 = binary right to left
122 Comments are introduced by a '#' symbol and are terminated by
123 the first newline character. The standard \*(OQ/*\*(CQ and \*(OQ*/\*(CQ
124 comment delimiters are not supported and will result in a
130 When \fIawk\fR reads a line from the current input file, the
131 record is automatically separated into \*(OQfields.\*(CQ A field is
132 simply a string of consecutive characters delimited by either
133 the beginning or end of line, or a \*(OQfield separator\*(CQ character.
134 Initially, the field separators are the space and tab character.
135 The special unary operator '$' is used to reference one of the
136 fields in the current input record (line). The fields are
137 numbered sequentially starting at 1. The expression \*(OQ$0\*(CQ
138 references the entire input line.
140 Similarly, the \*(OQrecord separator\*(CQ is used to determine the end
141 of an input \*(OQline,\*(CQ initially the newline character. The field
142 and record separators may be changed programatically by one of
143 the actions and will remain in effect until changed again.
145 Multiple (up to 10) field separators are allowed at a time, but
146 only one record separator.
148 Fields behave exactly like strings; and can be used in the same
149 context as a character array. These \*(OQarrays\*(CQ can be considered
150 to have been declared as:
153 ~~~~~char ($n)[ 128 ];
156 In other words, they are 128 bytes long. Notice that the
157 parentheses are necessary because the operators [] and $
158 associate from right to left; without them, the statement
159 would have parsed as:
162 ~~~~~char $(1[ 128 ]);
165 which is obviously ridiculous.
167 If the contents of one of these field arrays is altered, the
168 \*(OQ$0\*(CQ field will reflect this change. For example, this
175 will change the first character of the fourth field to an upper-
176 case letter 'A'. Then, when the following input line:
179 ~~~~~120 PRINT "Name address Zip"
182 is processed, it would be printed as:
185 ~~~~~120 PRINT "Name Address Zip"
188 Fields may also be modified with the strcpy() function (see
189 below). For example, the expression:
191 ~~~~~strcpy( $4, "Addr." );
193 applied to the same line above would yield:
195 ~~~~~120 PRINT "Name Addr. Zip"
197 .SS "Predefined Variables"
199 The following variables are pre-defined:
204 FS Field separator (see below).
206 RS Record separator (see below also).
208 NF Number of fields in current input record (line).
210 NR Number of records processed thus far.
212 FILENAME Name of current input file.
214 BEGIN A special <pattern> that matches the beginning of input text.
216 END A special <pattern> that matches the end of input text.
219 \fIAwk\fR also provides some useful built-in functions for string
220 manipulation and printing:
225 print(arg) Simple printing of strings only, terminated by '\\n'.
227 printf(arg...) Exactly the printf() function from C.
229 getline() Reads the next record and returns 0 on end of file.
231 nextfile() Closes the current input file and begins processing the next file
233 strlen(s) Returns the length of its string argument.
235 strcpy(s,t) Copies the string \*(OQt\*(CQ to the string \*(OQs\*(CQ.
237 strcmp(s,t) Compares the \*(OQs\*(CQ to \*(OQt\*(CQ and returns 0 if they match.
239 toupper(c) Returns its character argument converted to upper-case.
241 tolower(c) Returns its character argument converted to lower-case.
243 match(s,@re@) Compares the string \*(OQs\*(CQ to the regular expression \*(OQre\*(CQ and
244 returns the number of matches found (zero if none).
248 \fIAwk\fR was written by Saeko Hirabauashi and Kouichi Hirabayashi.