9103 opengroup acknowledgement should be properly formatted in man pages
[unleashed.git] / usr / src / man / man1 / awk.1
blobc5bf3da77052f64a0613819e784db6a4dadd25b1
1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document.  The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
45 .\" Copyright (c) 2005, Sun Microsystems, Inc.  All Rights Reserved
46 .\"
47 .TH AWK 1 "Jun 22, 2005"
48 .SH NAME
49 awk \- pattern scanning and processing language
50 .SH SYNOPSIS
51 .LP
52 .nf
53 \fB/usr/bin/awk\fR [\fB-f\fR \fIprogfile\fR] [\fB-F\fIc\fR\fR] [' \fIprog\fR '] [\fIparameters\fR]
54      [\fIfilename\fR]...
55 .fi
57 .LP
58 .nf
59 \fB/usr/xpg4/bin/awk\fR [\fB-F\fR\fIcERE\fR] [\fB-v\fR \fIassignment\fR]... \fI\&'program'\fR \fB-f\fR \fIprogfile\fR...
60      [\fIargument\fR]...
61 .fi
63 .SH DESCRIPTION
64 .sp
65 .LP
66 The \fB/usr/xpg4/bin/awk\fR utility is described on the \fBnawk\fR(1) manual
67 page.
68 .sp
69 .LP
70 The \fB/usr/bin/awk\fR utility scans each input \fIfilename\fR for lines that
71 match any of a set of patterns specified in \fIprog\fR. The \fIprog\fR string
72 must be enclosed in single quotes (\fB a\'\fR) to protect it from the shell.
73 For each pattern in \fIprog\fR there can be an associated action performed when
74 a line of a \fIfilename\fR matches the pattern. The set of pattern-action
75 statements can appear literally as \fIprog\fR or in a file specified with the
76 \fB-f\fR\fI progfile\fR option. Input files are read in order; if there are no
77 files, the standard input is read. The file name \fB\&'\(mi'\fR means the
78 standard input.
79 .SH OPTIONS
80 .sp
81 .LP
82 The following options are supported:
83 .sp
84 .ne 2
85 .na
86 \fB\fB-f\fR\fI progfile\fR \fR
87 .ad
88 .RS 16n
89 \fBawk\fR uses the set of patterns it reads from \fIprogfile\fR.
90 .RE
92 .sp
93 .ne 2
94 .na
95 \fB\fB-F\fR\fIc\fR \fR
96 .ad
97 .RS 16n
98 Uses the character \fIc\fR as the field separator (FS) character.  See the
99 discussion of \fBFS\fR below.
102 .SH USAGE
103 .SS "Input Lines"
106 Each input line is matched against the pattern portion of every pattern-action
107 statement; the associated action is performed for each matched pattern. Any
108 \fIfilename\fR of the form \fIvar=value\fR is treated as an assignment, not a
109 filename, and is executed at the time it would have been opened if it were a
110 filename. \fIVariables\fR assigned in this manner are not available inside a
111 \fBBEGIN\fR rule, and are assigned after previously specified files have been
112 read.
115 An input line is normally made up of fields separated by white spaces. (This
116 default can be changed by using the \fBFS\fR built-in variable or the
117 \fB-F\fR\fIc\fR option.) The default is to ignore leading blanks and to
118 separate fields by blanks and/or tab characters. However, if \fBFS\fR is
119 assigned a value that does not include any of the white spaces, then leading
120 blanks are not ignored. The fields are denoted \fB$1\fR, \fB$2\fR,
121 \fB\&.\|.\|.\fR\|; \fB$0\fR refers to the entire line.
122 .SS "Pattern-action Statements"
125 A pattern-action statement has the form:
127 .in +2
129 \fIpattern\fR\fB { \fR\fIaction\fR\fB } \fR
131 .in -2
136 Either pattern or action can be omitted. If there is no action, the matching
137 line is printed. If there is no pattern, the action is performed on every input
138 line. Pattern-action statements are separated by newlines or semicolons.
141 Patterns are arbitrary Boolean combinations ( \fB!\fR, ||, \fB&&\fR, and
142 parentheses) of relational expressions and regular expressions. A relational
143 expression is one of the following:
145 .in +2
147 \fIexpression relop expression
148 expression matchop regular_expression\fR
150 .in -2
154 where a \fIrelop\fR is any of the six relational operators in C, and a
155 \fImatchop\fR is either \fB~\fR (contains) or \fB!~\fR (does not contain). An
156 \fIexpression\fR is an arithmetic expression, a relational expression, the
157 special expression
159 .in +2
161 \fIvar \fRin \fIarray\fR
163 .in -2
167 or a Boolean combination of these.
170 Regular expressions are as in \fBegrep\fR(1). In patterns they must be
171 surrounded by slashes. Isolated regular expressions in a pattern apply to the
172 entire line. Regular expressions can also occur in relational expressions. A
173 pattern can consist of two patterns separated by a comma; in this case, the
174 action is performed for all lines between the occurrence of the first pattern
175 to the occurrence of the second pattern.
178 The special patterns \fBBEGIN\fR and \fBEND\fR can be used to capture control
179 before the first input line has been read and after the last input line has
180 been read respectively. These keywords do not combine with any other patterns.
181 .SS "Built-in Variables"
184 Built-in variables include:
186 .ne 2
188 \fB\fBFILENAME\fR \fR
190 .RS 13n
191 name of the current input file
195 .ne 2
197 \fB\fBFS\fR \fR
199 .RS 13n
200 input field separator regular expression (default blank and tab)
204 .ne 2
206 \fB\fBNF\fR \fR
208 .RS 13n
209 number of fields in the current record
213 .ne 2
215 \fB\fBNR\fR \fR
217 .RS 13n
218 ordinal number of the current record
222 .ne 2
224 \fB\fBOFMT\fR \fR
226 .RS 13n
227 output format for numbers (default \fB%.6g\fR)
231 .ne 2
233 \fB\fBOFS\fR \fR
235 .RS 13n
236 output field separator (default blank)
240 .ne 2
242 \fB\fBORS\fR \fR
244 .RS 13n
245 output record separator (default new-line)
249 .ne 2
251 \fB\fBRS\fR \fR
253 .RS 13n
254 input record separator (default new-line)
259 An action is a sequence of statements. A statement can be one of the following:
261 .in +2
263 if ( \fIexpression\fR ) \fIstatement\fR [ else \fIstatement\fR ]
264 while ( \fIexpression\fR ) \fIstatement\fR
265 do \fIstatement\fR while ( \fIexpression\fR )
266 for ( \fIexpression\fR ; \fIexpression\fR ; \fIexpression\fR ) \fIstatement\fR
267 for ( \fIvar\fR in \fIarray\fR ) \fIstatement\fR
268 break
269 continue
270 { [ \fIstatement\fR ] .\|.\|. }
271 \fIexpression\fR      # commonly variable = expression
272 print [ \fIexpression-list\fR ] [ >\fIexpression\fR ]
273 printf format [ ,\fIexpression-list\fR ] [ >\fIexpression\fR ]
274 next            # skip remaining patterns on this input line
275 exit [expr]     # skip the rest of the input; exit status is expr
277 .in -2
281 Statements are terminated by semicolons, newlines, or right braces. An empty
282 expression-list stands for the whole input line. Expressions take on string or
283 numeric values as appropriate, and are built using the operators \fB+\fR,
284 \fB\(mi\fR, \fB*\fR, \fB/\fR, \fB%\fR, \fB^\fR and concatenation (indicated by
285 a blank). The operators \fB++\fR, \fB\(mi\(mi\fR, \fB+=\fR, \fB\(mi=\fR,
286 \fB*=\fR, \fB/=\fR, \fB%=\fR, \fB^=\fR, \fB>\fR, \fB>=\fR, \fB<\fR, \fB<=\fR,
287 \fB==\fR, \fB!=\fR, and \fB?:\fR are also available in expressions. Variables
288 can be scalars, array elements (denoted x[i]), or fields. Variables are
289 initialized to the null string or zero. Array subscripts can be any string, not
290 necessarily numeric; this allows for a form of associative memory. String
291 constants are quoted (\fB""\fR), with the usual C escapes recognized within.
294 The \fBprint\fR statement prints its arguments on the standard output, or on a
295 file if \fB>\fR\fIexpression\fR is present, or on a pipe if '\fB|\fR\fIcmd\fR'
296 is present. The output resulted from the print statement is terminated by the
297 output record separator with each argument separated by the current output
298 field separator. The \fBprintf\fR statement formats its expression list
299 according to the format (see \fBprintf\fR(3C)).
300 .SS "Built-in Functions"
303 The arithmetic functions are as follows:
305 .ne 2
307 \fB\fBcos\fR(\fIx\fR)\fR
309 .RS 11n
310 Return cosine of \fIx\fR, where \fIx\fR is in radians. (In
311 \fB/usr/xpg4/bin/awk\fR only. See \fBnawk\fR(1).)
315 .ne 2
317 \fB\fBsin\fR(\fIx\fR)\fR
319 .RS 11n
320 Return sine of \fIx\fR, where \fIx\fR is in radians. (In
321 \fB/usr/xpg4/bin/awk\fR only. See \fBnawk\fR(1).)
325 .ne 2
327 \fB\fBexp\fR(\fIx\fR)\fR
329 .RS 11n
330 Return the exponential function of \fIx\fR.
334 .ne 2
336 \fB\fBlog\fR(\fIx\fR)\fR
338 .RS 11n
339 Return the natural logarithm of \fIx\fR.
343 .ne 2
345 \fB\fBsqrt\fR(\fIx\fR)\fR
347 .RS 11n
348 Return the square root of \fIx\fR.
352 .ne 2
354 \fB\fBint\fR(\fIx\fR)\fR
356 .RS 11n
357 Truncate its argument to an integer. It is truncated toward \fB0\fR when
358 \fIx\fR >\fB 0\fR.
363 The string functions are as follows:
365 .ne 2
367 \fB\fBindex(\fR\fIs\fR\fB, \fR\fIt\fR\fB)\fR\fR
369 .sp .6
370 .RS 4n
371 Return the position in string \fIs\fR where string \fIt\fR first occurs, or
372 \fB0\fR if it does not occur at all.
376 .ne 2
378 \fB\fBint(\fR\fIs\fR\fB)\fR\fR
380 .sp .6
381 .RS 4n
382 truncates \fIs\fR to an integer value. If \fIs\fR is not specified, $0 is used.
386 .ne 2
388 \fB\fBlength(\fR\fIs\fR\fB)\fR\fR
390 .sp .6
391 .RS 4n
392 Return the length of its argument taken as a string, or of the whole line if
393 there is no argument.
397 .ne 2
399 \fB\fBsplit(\fR\fIs\fR, \fIa\fR, \fIfs\fR\fB)\fR\fR
401 .sp .6
402 .RS 4n
403 Split the string \fIs\fR into array elements \fIa\fR[\fI1\fR],
404 \fIa\fR[\fI2\fR], \|.\|.\|. \fIa\fR[\fIn\fR], and returns \fIn\fR. The
405 separation is done with the regular expression \fIfs\fR or with the field
406 separator \fBFS\fR if \fIfs\fR is not given.
410 .ne 2
412 \fB\fBsprintf(\fR\fIfmt\fR, \fIexpr\fR, \fIexpr\fR,\|.\|.\|.\|\fB)\fR\fR
414 .sp .6
415 .RS 4n
416 Format the expressions according to the \fBprintf\fR(3C) format given by
417 \fIfmt\fR and returns the resulting string.
421 .ne 2
423 \fB\fBsubstr(\fR\fIs\fR, \fIm\fR, \fIn\fR\fB)\fR\fR
425 .sp .6
426 .RS 4n
427 returns the \fIn\fR-character substring of \fIs\fR that begins at position
428 \fIm\fR.
433 The input/output function is as follows:
435 .ne 2
437 \fB\fBgetline\fR\fR
439 .RS 11n
440 Set \fB$0\fR to the next input record from the current input file.
441 \fBgetline\fR returns \fB1\fR for successful input, \fB0\fR for end of file,
442 and \fB\(mi1\fR for an error.
445 .SS "Large File Behavior"
448 See \fBlargefile\fR(5) for the description of the behavior of \fBawk\fR when
449 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
450 .SH EXAMPLES
452 \fBExample 1 \fRPrinting Lines Longer Than 72 Characters
455 The following example is an \fBawk\fR script that can be executed by an \fBawk
456 -f examplescript\fR style command. It prints lines longer than seventy two
457 characters:
460 .in +2
462 \fBlength > 72\fR
464 .in -2
468 \fBExample 2 \fRPrinting Fields in Opposite Order
471 The following example is an \fBawk\fR script that can be executed by an \fBawk
472 -f examplescript\fR style command. It prints the first two fields in opposite
473 order:
476 .in +2
478 \fB{ print $2, $1 }\fR
480 .in -2
484 \fBExample 3 \fRPrinting Fields in Opposite Order with the Input Fields
485 Separated
488 The following example is an \fBawk\fR script that can be executed by an \fBawk
489 -f examplescript\fR style command. It prints the first two input fields in
490 opposite order, separated by a comma, blanks or tabs:
493 .in +2
495 \fBBEGIN { FS = ",[ \et]*|[ \et]+" }
496       { print $2, $1 }\fR
498 .in -2
502 \fBExample 4 \fRAdding Up the First Column, Printing the Sum and Average
505 The following example is an \fBawk\fR script that can be executed by an \fBawk
506 -f examplescript\fR style command.  It adds up the first column, and prints the
507 sum and average:
510 .in +2
512 \fB{ s += $1 }
513 END  { print "sum is", s, " average is", s/NR }\fR
515 .in -2
519 \fBExample 5 \fRPrinting Fields in Reverse Order
522 The following example is an \fBawk\fR script that can be executed by an \fBawk
523 -f examplescript\fR style command. It prints fields in reverse order:
526 .in +2
528 \fB{ for (i = NF; i > 0; \(mi\(mii) print $i }\fR
530 .in -2
534 \fBExample 6 \fRPrinting All lines Between \fBstart/stop\fR Pairs
537 The following example is an \fBawk\fR script that can be executed by an \fBawk
538 -f examplescript\fR style command. It prints all lines between start/stop
539 pairs.
542 .in +2
544 \fB/start/, /stop/\fR
546 .in -2
550 \fBExample 7 \fRPrinting All Lines Whose First Field is Different from the
551 Previous One
554 The following example is an \fBawk\fR script that can be executed by an \fBawk
555 -f examplescript\fR style command. It prints all lines whose first field is
556 different from the previous one.
559 .in +2
561 \fB$1 != prev { print; prev = $1 }\fR
563 .in -2
567 \fBExample 8 \fRPrinting a File and Filling in Page numbers
570 The following example is an \fBawk\fR script that can be executed by an \fBawk
571 -f examplescript\fR style command. It prints a file and fills in page numbers
572 starting at 5:
575 .in +2
577 \fB/Page/       { $2 = n++; }
578            { print }\fR
580 .in -2
584 \fBExample 9 \fRPrinting a File and Numbering Its Pages
587 Assuming this program is in a file named \fBprog\fR, the following example
588 prints the file \fBinput\fR numbering its pages starting at \fB5\fR:
591 .in +2
593 example% \fBawk -f prog n=5 input\fR
595 .in -2
598 .SH ENVIRONMENT VARIABLES
601 See \fBenviron\fR(5) for descriptions of the following environment variables
602 that affect the execution of \fBawk\fR: \fBLANG\fR, \fBLC_ALL\fR,
603 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, \fBNLSPATH\fR, and
604 \fBPATH\fR.
606 .ne 2
608 \fB\fBLC_NUMERIC\fR\fR
610 .RS 14n
611 Determine the radix character used when interpreting numeric input, performing
612 conversions between numeric and string values and formatting numeric output.
613 Regardless of locale, the period character (the decimal-point character of the
614 POSIX locale) is the decimal-point character recognized in processing \fBawk\fR
615 programs (including assignments in command-line arguments).
618 .SH ATTRIBUTES
621 See \fBattributes\fR(5) for descriptions of the following attributes:
622 .SS "/usr/bin/awk"
627 box;
628 c | c
629 l | l .
630 ATTRIBUTE TYPE  ATTRIBUTE VALUE
632 CSI     Not Enabled
635 .SS "/usr/xpg4/bin/awk"
640 box;
641 c | c
642 l | l .
643 ATTRIBUTE TYPE  ATTRIBUTE VALUE
645 CSI     Enabled
647 Interface Stability     Standard
650 .SH SEE ALSO
653 \fBegrep\fR(1), \fBgrep\fR(1), \fBnawk\fR(1), \fBsed\fR(1), \fBprintf\fR(3C),
654 \fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBstandards\fR(5)
655 .SH NOTES
658 Input white space is not preserved on output if fields are involved.
661 There are no explicit conversions between numbers and strings. To force an
662 expression to be treated as a number, add \fB0\fR to it. To force an expression
663 to be treated as a string, concatenate the null string (\fB""\fR) to it.