1 OAWK(1) User Commands OAWK(1)
2
3
4
5 NAME
6 oawk - (older) pattern scanning and processing language
7
8 SYNOPSIS
9 /usr/bin/oawk [-f progfile] [-Fc] [' prog '] [parameters]
10 [filename]...
11
12
13 DESCRIPTION
14 This command is now obsolete, and will be removed from illumos at some
15 point.
16
17
18 The /usr/bin/oawk utility scans each input filename for lines that
19 match any of a set of patterns specified in prog. The prog string must
20 be enclosed in single quotes ( a') to protect it from the shell. For
21 each pattern in prog there can be an associated action performed when a
22 line of a filename matches the pattern. The set of pattern-action
23 statements can appear literally as prog or in a file specified with the
24 -f progfile option. Input files are read in order; if there are no
25 files, the standard input is read. The file name '-' means the standard
26 input.
27
28 OPTIONS
29 The following options are supported:
30
31 -f progfile
32 oawk uses the set of patterns it reads from progfile.
33
34
35 -Fc
36 Uses the character c as the field separator (FS)
37 character. See the discussion of FS below.
38
39
40 USAGE
41 Input Lines
42 Each input line is matched against the pattern portion of every
43 pattern-action statement; the associated action is performed for each
44 matched pattern. Any filename of the form var=value is treated as an
45 assignment, not a filename, and is executed at the time it would have
46 been opened if it were a filename. Variables assigned in this manner
47 are not available inside a BEGIN rule, and are assigned after
48 previously specified files have been read.
49
50
51 An input line is normally made up of fields separated by white spaces.
52 (This default can be changed by using the FS built-in variable or the
53 -Fc option.) The default is to ignore leading blanks and to separate
54 fields by blanks and/or tab characters. However, if FS is assigned a
55 value that does not include any of the white spaces, then leading
56 blanks are not ignored. The fields are denoted $1, $2, ...; $0 refers
57 to the entire line.
58
59 Pattern-action Statements
60 A pattern-action statement has the form:
61
62 pattern { action }
63
64
65
66
67 Either pattern or action can be omitted. If there is no action, the
68 matching line is printed. If there is no pattern, the action is
69 performed on every input line. Pattern-action statements are separated
70 by newlines or semicolons.
71
72
73 Patterns are arbitrary Boolean combinations ( !, ||, &&, and
74 parentheses) of relational expressions and regular expressions. A
75 relational expression is one of the following:
76
77 expression relop expression
78 expression matchop regular_expression
79
80
81
82 where a relop is any of the six relational operators in C, and a
83 matchop is either ~ (contains) or !~ (does not contain). An expression
84 is an arithmetic expression, a relational expression, the special
85 expression
86
87 var in array
88
89
90
91 or a Boolean combination of these.
92
93
94 Regular expressions are as in egrep(1). In patterns they must be
95 surrounded by slashes. Isolated regular expressions in a pattern apply
96 to the entire line. Regular expressions can also occur in relational
97 expressions. A pattern can consist of two patterns separated by a
98 comma; in this case, the action is performed for all lines between the
99 occurrence of the first pattern to the occurrence of the second
100 pattern.
101
102
103 The special patterns BEGIN and END can be used to capture control
104 before the first input line has been read and after the last input line
105 has been read respectively. These keywords do not combine with any
106 other patterns.
107
108 Built-in Variables
109 Built-in variables include:
110
111 FILENAME
112 name of the current input file
113
114
115 FS
116 input field separator regular expression (default blank
117 and tab)
118
119
120 NF
121 number of fields in the current record
122
123
124 NR
125 ordinal number of the current record
126
127
128 OFMT
129 output format for numbers (default %.6g)
130
131
132 OFS
133 output field separator (default blank)
134
135
136 ORS
137 output record separator (default new-line)
138
139
140 RS
141 input record separator (default new-line)
142
143
144
145 An action is a sequence of statements. A statement can be one of the
146 following:
147
148 if ( expression ) statement [ else statement ]
149 while ( expression ) statement
150 do statement while ( expression )
151 for ( expression ; expression ; expression ) statement
152 for ( var in array ) statement
153 break
154 continue
155 { [ statement ] ... }
156 expression # commonly variable = expression
157 print [ expression-list ] [ >expression ]
158 printf format [ ,expression-list ] [ >expression ]
159 next # skip remaining patterns on this input line
160 exit [expr] # skip the rest of the input; exit status is expr
161
162
163
164 Statements are terminated by semicolons, newlines, or right braces. An
165 empty expression-list stands for the whole input line. Expressions take
166 on string or numeric values as appropriate, and are built using the
167 operators +, -, *, /, %, ^ and concatenation (indicated by a blank).
168 The operators ++, --, +=, -=, *=, /=, %=, ^=, >, >=, <, <=, ==, !=, and
169 ?: are also available in expressions. Variables can be scalars, array
170 elements (denoted x[i]), or fields. Variables are initialized to the
171 null string or zero. Array subscripts can be any string, not
172 necessarily numeric; this allows for a form of associative memory.
173 String constants are quoted (""), with the usual C escapes recognized
174 within.
175
176
177 The print statement prints its arguments on the standard output, or on
178 a file if >expression is present, or on a pipe if '|cmd' is present.
179 The output resulted from the print statement is terminated by the
180 output record separator with each argument separated by the current
181 output field separator. The printf statement formats its expression
182 list according to the format (see printf(3C)).
183
184 Built-in Functions
185 The arithmetic functions are as follows:
186
187 exp(x)
188 Return the exponential function of x.
189
190
191 log(x)
192 Return the natural logarithm of x.
193
194
195 sqrt(x)
196 Return the square root of x.
197
198
199 int(x)
200 Truncate its argument to an integer. It is truncated toward
201 0 when x > 0.
202
203
204
205 The string functions are as follows:
206
207 index(s, t)
208
209 Return the position in string s where string t first occurs, or 0
210 if it does not occur at all.
211
212
213 int(s)
214
215 truncates s to an integer value. If s is not specified, $0 is used.
216
217
218 length(s)
219
220 Return the length of its argument taken as a string, or of the
221 whole line if there is no argument.
222
223
224 split(s, a, fs)
225
226 Split the string s into array elements a[1], a[2], ... a[n], and
227 returns n. The separation is done with the regular expression fs or
228 with the field separator FS if fs is not given.
229
230
231 sprintf(fmt, expr, expr,...)
232
233 Format the expressions according to the printf(3C) format given by
234 fmt and returns the resulting string.
235
236
237 substr(s, m, n)
238
239 returns the n-character substring of s that begins at position m.
240
241
242
243 The input/output function is as follows:
244
245 getline
246 Set $0 to the next input record from the current input file.
247 getline returns 1 for successful input, 0 for end of file,
248 and -1 for an error.
249
250
251 Large File Behavior
252 See largefile(5) for the description of the behavior of oawk when
253 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
254
255 EXAMPLES
256 Example 1 Printing Lines Longer Than 72 Characters
257
258
259 The following example is an oawk script that can be executed by an oawk
260 -f examplescript style command. It prints lines longer than seventy two
261 characters:
262
263
264 length > 72
265
266
267
268 Example 2 Printing Fields in Opposite Order
269
270
271 The following example is an oawk script that can be executed by an oawk
272 -f examplescript style command. It prints the first two fields in
273 opposite order:
274
275
276 { print $2, $1 }
277
278
279
280 Example 3 Printing Fields in Opposite Order with the Input Fields
281 Separated
282
283
284 The following example is an oawk script that can be executed by an oawk
285 -f examplescript style command. It prints the first two input fields in
286 opposite order, separated by a comma, blanks or tabs:
287
288
289 BEGIN { FS = ",[ \t]*|[ \t]+" }
290 { print $2, $1 }
291
292
293
294 Example 4 Adding Up the First Column, Printing the Sum and Average
295
296
297 The following example is an oawk script that can be executed by an oawk
298 -f examplescript style command. It adds up the first column, and
299 prints the sum and average:
300
301
302 { s += $1 }
303 END { print "sum is", s, " average is", s/NR }
304
305
306
307 Example 5 Printing Fields in Reverse Order
308
309
310 The following example is an oawk script that can be executed by an oawk
311 -f examplescript style command. It prints fields in reverse order:
312
313
314 { for (i = NF; i > 0; --i) print $i }
315
316
317
318 Example 6 Printing All lines Between start/stop Pairs
319
320
321 The following example is an oawk script that can be executed by an oawk
322 -f examplescript style command. It prints all lines between start/stop
323 pairs.
324
325
326 /start/, /stop/
327
328
329
330 Example 7 Printing All Lines Whose First Field is Different from the
331 Previous One
332
333
334 The following example is an oawk script that can be executed by an oawk
335 -f examplescript style command. It prints all lines whose first field
336 is different from the previous one.
337
338
339 $1 != prev { print; prev = $1 }
340
341
342
343 Example 8 Printing a File and Filling in Page numbers
344
345
346 The following example is an oawk script that can be executed by an oawk
347 -f examplescript style command. It prints a file and fills in page
348 numbers starting at 5:
349
350
351 /Page/ { $2 = n++; }
352 { print }
353
354
355
356 Example 9 Printing a File and Numbering Its Pages
357
358
359 Assuming this program is in a file named prog, the following example
360 prints the file input numbering its pages starting at 5:
361
362
363 example% oawk -f prog n=5 input
364
365
366
367 ENVIRONMENT VARIABLES
368 See environ(5) for descriptions of the following environment variables
369 that affect the execution of oawk: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
370 LC_MESSAGES, NLSPATH, and PATH.
371
372 LC_NUMERIC
373 Determine the radix character used when interpreting
374 numeric input, performing conversions between numeric and
375 string values and formatting numeric output. Regardless
376 of locale, the period character (the decimal-point
377 character of the POSIX locale) is the decimal-point
378 character recognized in processing oawk programs
379 (including assignments in command-line arguments).
380
381
382 ATTRIBUTES
383 See attributes(5) for descriptions of the following attributes:
384
385 /usr/bin/oawk
386
387 +---------------+-----------------+
388 |ATTRIBUTE TYPE | ATTRIBUTE VALUE |
389 +---------------+-----------------+
390 |CSI | Not Enabled |
391 +---------------+-----------------+
392
393 SEE ALSO
394 egrep(1), grep(1), awk(1), sed(1), printf(3C), attributes(5),
395 environ(5), largefile(5), standards(5)
396
397 NOTES
398 Input white space is not preserved on output if fields are involved.
399
400
401 There are no explicit conversions between numbers and strings. To force
402 an expression to be treated as a number, add 0 to it. To force an
403 expression to be treated as a string, concatenate the null string ("")
404 to it.
405
406
407
408 April 20, 2020 OAWK(1)