summaryrefslogtreecommitdiff
path: root/usr.bin/awk/awk.1
blob: 65ba69c068a753f36ee21f4dd2653da86decd377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
.\"	$OpenBSD: awk.1,v 1.9 2002/07/17 21:48:56 millert Exp $
.\" EX/EE is a Bd
.Dd June 29, 1996
.Dt AWK 1
.Os
.Sh NAME
.Nm awk
.Nd pattern-directed scanning and processing language
.Sh SYNOPSIS
.Nm awk
.Op Fl F Ar fs
.Op Fl v Ar var=value
.Op Fl safe
.Op Fl mr Ar n
.Op Fl mf Ar n
.Op Ar prog | Fl f Ar progfile
.Ar
.Nm nawk
.Ar ...
.Sh DESCRIPTION
.Nm
scans each input
.Ar file
for lines that match any of a set of patterns specified literally in
.Ar prog
or in one or more files
specified as
.Fl f Ar progfile .
With each pattern
there can be an associated action that will be performed
when a line of a
.Ar file
matches the pattern.
Each line is matched against the
pattern portion of every pattern-action statement;
the associated action is performed for each matched pattern.
The file name
.Sq Pa \-
means the standard input.
Any
.Ar file
of the form
.Ar var=value
is treated as an assignment, not a filename,
and is executed at the time it would have been opened if it were a filename.
The option
.Fl v
followed by
.Ar var=value
is an assignment to be done before
.Ar prog
is executed;
any number of
.Fl v
options may be present.
The
.Fl F Ar fs
option defines the input field separator to be the regular expression
.Ar fs .
The
.Fl safe
option disables file output
.Po
.Ic print Ic > ,
.Ic print Ic >> ,
.Pc
process creation
.Po
.Ar cmd Ic \&| getline ,
.Ic print \&| , system
.Pc
and access to the environment
.Pq Va ENVIRON .
This
is a first (and not very reliable) approximation to a
.Dq safe
version of
.Nm awk .
.Pp
An input line is normally made up of fields separated by whitespace,
or by regular expression
.Va FS .
The fields are denoted
.Va $1 , $2 , ... ,
while
.Va $0
refers to the entire line.
If
.Va FS
is null, the input line is split into one field per character.
.Pp
To compensate for inadequate implementation of storage management,
the
.Fl mr
option can be used to set the maximum size of the input record,
and the
.Fl mf
option to set the maximum number of fields.
.Pp
A pattern-action statement has the form
.Pp
.D1 Ar pattern Ic \&{ Ar action Ic \&}
.Pp
A missing
.Ic \&{ Ar action Ic \&}
means print the line;
a missing pattern always matches.
Pattern-action statements are separated by newlines or semicolons.
.Pp
An action is a sequence of statements.
A statement can be one of the following:
.Pp
.Bd -unfilled -offset indent
.Ic if ( Xo
.Ar expression ) statement \&
.Op Ic else Ar statement
.Xc
.Ic while ( Ar expression ) statement
.Ic for ( Xo
.Ar expression ; expression ; expression ) statement
.Xc
.Ic for ( Xo
.Ar var Ic in Ar array ) statement
.Xc
.Ic do Ar statement Ic while ( Ar expression )
.Ic break
.Ic continue
.Ic { Oo Ar statement ... Oc Ic \& }
.Ar expression Xo
.No "# commonly" \&
.Ar var Ic = Ar expression
.Xc
.Ic print Xo
.Op Ar expression-list
.Op Ic > Ns Ar expression
.Xc
.Ic printf Ar format Xo
.Op Ar ... , expression-list
.Op Ic > Ns Ar expression
.Xc
.Ic return Op Ar expression
.Ic next Xo
.No "# skip remaining patterns on this input line"
.Xc
.Ic nextfile Xo
.No "# skip rest of this file, open next, start at top"
.Xc
.Ic delete Ar array Ns Xo
.Ic \&[ Ns Ar expression Ns Ic \&]
.No \& "# delete an array element"
.Xc
.Ic delete Ar array Xo
.No "# delete all elements of array"
.Xc
.Ic exit Xo
.Op Ar expression
.No \& "# exit immediately; status is" Ar expression
.Xc
.Ed
.Pp
Statements are terminated by
semicolons, newlines or right braces.
An empty
.Ar expression-list
stands for
.Ar $0 .
String constants are quoted
.Li \&"" ,
with the usual C escapes recognized within.
Expressions take on string or numeric values as appropriate,
and are built using the operators
.Ic + \- * / % ^
(exponentiation), and concatenation (indicated by whitespace).
The operators
.Ic ! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:
are also available in expressions.
Variables may be scalars, array elements
(denoted
.Li x[i] )
or fields.
Variables are initialized to the null string.
Array subscripts may be any string,
not necessarily numeric;
this allows for a form of associative memory.
Multiple subscripts such as
.Li [i,j,k]
are permitted; the constituents are concatenated,
separated by the value of
.Va SUBSEP .
.Pp
The
.Ic print
statement prints its arguments on the standard output
(or on a file if
.Ic > Ns Ar file
or
.Ic >> Ns Ar file
is present or on a pipe if
.Ic \&| Ar cmd
is present), separated by the current output field separator,
and terminated by the output record separator.
.Ar file
and
.Ar cmd
may be literal names or parenthesized expressions;
identical string values in different statements denote
the same open file.
The
.Ic printf
statement formats its expression list according to the format
(see
.Xr printf 3 .
The built-in function
.Fn close expr
closes the file or pipe
.Fa expr .
The built-in function
.Fn fflush expr
flushes any buffered output for the file or pipe
.Fa expr .
.Pp
The mathematical functions
.Fn exp ,
.Fn log ,
.Fn sqrt ,
.Fn sin ,
.Fn cos ,
and
.Fn atan2
are built in.
Other built-in functions:
.Pp
.Bl -tag -width Fn
.It Fn length
the length of its argument
taken as a string,
or of
.Va $0
if no argument.
.It Fn rand
random number on (0,1)
.It Fn srand
sets seed for
.Fn rand
and returns the previous seed.
.It Fn int
truncates to an integer value.
.It Fn substr s m n
the
.Fa n Ns No -character
substring of
.Fa s
that begins at position
.Fa m
counted from 1.
.It Fn index s t
the position in
.Fa s
where the string
.Fa t
occurs, or 0 if it does not.
.It Fn match s r
the position in
.Fa s
where the regular expression
.Fa r
occurs, or 0 if it does not.
The variables
.Va RSTART
and
.Va RLENGTH
are set to the position and length of the matched string.
.It Fn split s a fs
splits the string
.Fa s
into array elements
.Va a[1] , a[2] , ... , a[n]
and returns
.Va n .
The separation is done with the regular expression
.Ar fs
or with the field separator
.Va FS
if
.Ar fs
is not given.
An empty string as field separator splits the string
into one array element per character.
.It Fn sub r t s
substitutes
.Fa t
for the first occurrence of the regular expression
.Fa r
in the string
.Fa s .
If
.Fa s
is not given,
.Va $0
is used.
.It Fn gsub r t s
same as
.Fn sub
except that all occurrences of the regular expression
are replaced;
.Fn sub
and
.Fn gsub
return the number of replacements.
.It Fn sprintf fmt expr ...
the string resulting from formatting
.Fa expr , ...
according to the
.Xr printf 3
format
.Fa fmt .
.It Fn system cmd
executes
.Fa cmd
and returns its exit status.
.It Fn tolower str
returns a copy of
.Fa str
with all upper-case characters translated to their
corresponding lower-case equivalents.
.It Fn toupper str
returns a copy of
.Fa str
with all lower-case characters translated to their
corresponding upper-case equivalents.
.El
.Pp
The
.Sq function
.Ic getline
sets
.Va $0
to the next input record from the current input file;
.Ic getline < Ar file
sets
.Va $0
to the next record from
.Ar file .
.Ic getline Va x
sets variable
.Va x
instead.
Finally,
.Ar cmd Ic \&| getline
pipes the output of
.Ar cmd
into
.Ic getline ;
each call of
.Ic getline
returns the next line of output from
.Ar cmd .
In all cases,
.Ic getline
returns 1 for a successful input,
0 for end of file, and \-1 for an error.
.Pp
Patterns are arbitrary Boolean combinations
(with
.Ic "! || &&" )
of regular expressions and
relational expressions.
Regular expressions are as in
.Xr egrep  1 .
Isolated regular expressions
in a pattern apply to the entire line.
Regular expressions may also occur in
relational expressions, using the operators
.Ic ~
and
.Ic !~ .
.Ic / Ns Ar re Ns Ic /
is a constant regular expression;
any string (constant or variable) may be used
as a regular expression, except in the position of an isolated regular expression
in a pattern.
.Pp
A pattern may consist of two patterns separated by a comma;
in this case, the action is performed for all lines
from an occurrence of the first pattern
though an occurrence of the second.
.Pp
A relational expression is one of the following:
.Bd -unfilled -offset indent
.Ar expression matchop regular-expression
.Ar expression relop expression
.Ar expression Ic in Ar array-name
.Ic \&( Ns Xo
.Ar expr , expr , \&... Ns Ic \&) in
.Ar \& array-name
.Xc
.Ed
where a
.Ar relop
is any of the six relational operators in C, and a
.Ar matchop
is either
.Ic ~
(matches)
or
.Ic !~
(does not match).
A conditional is an arithmetic expression,
a relational expression,
or a Boolean combination
of these.
.Pp
The special patterns
.Ic BEGIN
and
.Ic END
may be used to capture control before the first input line is read
and after the last.
.Ic BEGIN
and
.Ic END
do not combine with other patterns.
.Pp
Variable names with special meanings:
.Pp
.Bl -tag -width Va -compact
.It Va CONVFMT
conversion format used when converting numbers
(default
.Qq Li %.6g )
.It Va FS
regular expression used to separate fields; also settable
by option
.Fl F Ar fs .
.It Va NF
number of fields in the current record
.It Va NR
ordinal number of the current record
.It Va FNR
ordinal number of the current record in the current file
.It Va FILENAME
the name of the current input file
.It Va RS
input record separator (default newline)
.It Va OFS
output field separator (default blank)
.It Va ORS
output record separator (default newline)
.It Va OFMT
output format for numbers (default
.Qq Li %.6g )
.It Va SUBSEP
separates multiple subscripts (default 034)
.It Va ARGC
argument count, assignable
.It Va ARGV
argument array, assignable;
non-null members are taken as filenames
.It Va ENVIRON
array of environment variables; subscripts are names.
.El
.Pp
Functions may be defined (at the position of a pattern-action statement)
thusly:
.Pp
.Dl function foo(a, b, c) { ...; return x }
.Pp
Parameters are passed by value if scalar and by reference if array name;
functions may be called recursively.
Parameters are local to the function; all other variables are global.
Thus local variables may be created by providing excess parameters in
the function definition.
.Sh EXAMPLES
.Dl length($0) > 72
Print lines longer than 72 characters.
.Pp
.Dl { print $2, $1 }
Print first two fields in opposite order.
.Pp
.Bd -literal -offset indent
BEGIN { FS = ",[ \et]*|[ \et]+" }
      { print $2, $1 }
.Ed
Same, with input fields separated by comma and/or blanks and tabs.
.Pp
.Bd -literal -offset indent
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
.Ed
Add up first column, print sum and average.
.Pp
.Dl /start/, /stop/
Print all lines between start/stop pairs.
.Pp
.Bd -literal -offset indent
BEGIN { # Simulate echo(1)
        for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
        printf "\en"
        exit }
.Ed
.Sh SEE ALSO
.Xr lex 1 ,
.Xr sed 1
.Rs
.%A A. V. Aho
.%A B. W. Kernighan
.%A P. J. Weinberger
.%T The AWK Programming Language
.%I Addison-Wesley
.%D 1988
.%O ISBN 0-201-07981-X
.Re
.Sh HISTORY
AT&T
.Nm
by B. W. Kernighan was updated for
.Bx 4.4
and again in 1996.
.Sh BUGS
There are no explicit conversions between numbers and strings.
To force an expression to be treated as a number add 0 to it;
to force it to be treated as a string concatenate
.Li \&""
to it.
.Pp
The scope rules for variables in functions are a botch;
the syntax is worse.