2 .\" Copyright (c) 2005, Sun Microsystems, Inc. All Rights Reserved
3 .\" Copyright 1989 AT&T
4 .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
8 .\" This notice shall appear on any product containing this material.
9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH AWK 1 "Jun 22, 2005"
14 awk \- pattern scanning and processing language
18 \fB/usr/bin/awk\fR [\fB-f\fR \fIprogfile\fR] [\fB-F\fIc\fR\fR] [' \fIprog\fR '] [\fIparameters\fR]
24 \fB/usr/xpg4/bin/awk\fR [\fB-F\fR\fIcERE\fR] [\fB-v\fR \fIassignment\fR]... \fI\&'program'\fR \fB-f\fR \fIprogfile\fR...
31 The \fB/usr/xpg4/bin/awk\fR utility is described on the \fBnawk\fR(1) manual
35 The \fB/usr/bin/awk\fR utility scans each input \fIfilename\fR for lines that
36 match any of a set of patterns specified in \fIprog\fR. The \fIprog\fR string
37 must be enclosed in single quotes (\fB a\'\fR) to protect it from the shell.
38 For each pattern in \fIprog\fR there can be an associated action performed when
39 a line of a \fIfilename\fR matches the pattern. The set of pattern-action
40 statements can appear literally as \fIprog\fR or in a file specified with the
41 \fB-f\fR\fI progfile\fR option. Input files are read in order; if there are no
42 files, the standard input is read. The file name \fB\&'\(mi'\fR means the
47 The following options are supported:
51 \fB\fB-f\fR\fI progfile\fR \fR
54 \fBawk\fR uses the set of patterns it reads from \fIprogfile\fR.
60 \fB\fB-F\fR\fIc\fR \fR
63 Uses the character \fIc\fR as the field separator (FS) character. See the
64 discussion of \fBFS\fR below.
71 Each input line is matched against the pattern portion of every pattern-action
72 statement; the associated action is performed for each matched pattern. Any
73 \fIfilename\fR of the form \fIvar=value\fR is treated as an assignment, not a
74 filename, and is executed at the time it would have been opened if it were a
75 filename. \fIVariables\fR assigned in this manner are not available inside a
76 \fBBEGIN\fR rule, and are assigned after previously specified files have been
80 An input line is normally made up of fields separated by white spaces. (This
81 default can be changed by using the \fBFS\fR built-in variable or the
82 \fB-F\fR\fIc\fR option.) The default is to ignore leading blanks and to
83 separate fields by blanks and/or tab characters. However, if \fBFS\fR is
84 assigned a value that does not include any of the white spaces, then leading
85 blanks are not ignored. The fields are denoted \fB$1\fR, \fB$2\fR,
86 \fB\&.\|.\|.\fR\|; \fB$0\fR refers to the entire line.
87 .SS "Pattern-action Statements"
90 A pattern-action statement has the form:
94 \fIpattern\fR\fB { \fR\fIaction\fR\fB } \fR
101 Either pattern or action can be omitted. If there is no action, the matching
102 line is printed. If there is no pattern, the action is performed on every input
103 line. Pattern-action statements are separated by newlines or semicolons.
106 Patterns are arbitrary Boolean combinations ( \fB!\fR, ||, \fB&&\fR, and
107 parentheses) of relational expressions and regular expressions. A relational
108 expression is one of the following:
112 \fIexpression relop expression
113 expression matchop regular_expression\fR
119 where a \fIrelop\fR is any of the six relational operators in C, and a
120 \fImatchop\fR is either \fB~\fR (contains) or \fB!~\fR (does not contain). An
121 \fIexpression\fR is an arithmetic expression, a relational expression, the
126 \fIvar \fRin \fIarray\fR
132 or a Boolean combination of these.
135 Regular expressions are as in \fBegrep\fR(1). In patterns they must be
136 surrounded by slashes. Isolated regular expressions in a pattern apply to the
137 entire line. Regular expressions can also occur in relational expressions. A
138 pattern can consist of two patterns separated by a comma; in this case, the
139 action is performed for all lines between the occurrence of the first pattern
140 to the occurrence of the second pattern.
143 The special patterns \fBBEGIN\fR and \fBEND\fR can be used to capture control
144 before the first input line has been read and after the last input line has
145 been read respectively. These keywords do not combine with any other patterns.
146 .SS "Built-in Variables"
149 Built-in variables include:
153 \fB\fBFILENAME\fR \fR
156 name of the current input file
165 input field separator regular expression (default blank and tab)
174 number of fields in the current record
183 ordinal number of the current record
192 output format for numbers (default \fB%.6g\fR)
201 output field separator (default blank)
210 output record separator (default new-line)
219 input record separator (default new-line)
224 An action is a sequence of statements. A statement can be one of the following:
228 if ( \fIexpression\fR ) \fIstatement\fR [ else \fIstatement\fR ]
229 while ( \fIexpression\fR ) \fIstatement\fR
230 do \fIstatement\fR while ( \fIexpression\fR )
231 for ( \fIexpression\fR ; \fIexpression\fR ; \fIexpression\fR ) \fIstatement\fR
232 for ( \fIvar\fR in \fIarray\fR ) \fIstatement\fR
235 { [ \fIstatement\fR ] .\|.\|. }
236 \fIexpression\fR # commonly variable = expression
237 print [ \fIexpression-list\fR ] [ >\fIexpression\fR ]
238 printf format [ ,\fIexpression-list\fR ] [ >\fIexpression\fR ]
239 next # skip remaining patterns on this input line
240 exit [expr] # skip the rest of the input; exit status is expr
246 Statements are terminated by semicolons, newlines, or right braces. An empty
247 expression-list stands for the whole input line. Expressions take on string or
248 numeric values as appropriate, and are built using the operators \fB+\fR,
249 \fB\(mi\fR, \fB*\fR, \fB/\fR, \fB%\fR, \fB^\fR and concatenation (indicated by
250 a blank). The operators \fB++\fR, \fB\(mi\(mi\fR, \fB+=\fR, \fB\(mi=\fR,
251 \fB*=\fR, \fB/=\fR, \fB%=\fR, \fB^=\fR, \fB>\fR, \fB>=\fR, \fB<\fR, \fB<=\fR,
252 \fB==\fR, \fB!=\fR, and \fB?:\fR are also available in expressions. Variables
253 can be scalars, array elements (denoted x[i]), or fields. Variables are
254 initialized to the null string or zero. Array subscripts can be any string, not
255 necessarily numeric; this allows for a form of associative memory. String
256 constants are quoted (\fB""\fR), with the usual C escapes recognized within.
259 The \fBprint\fR statement prints its arguments on the standard output, or on a
260 file if \fB>\fR\fIexpression\fR is present, or on a pipe if '\fB|\fR\fIcmd\fR'
261 is present. The output resulted from the print statement is terminated by the
262 output record separator with each argument separated by the current output
263 field separator. The \fBprintf\fR statement formats its expression list
264 according to the format (see \fBprintf\fR(3C)).
265 .SS "Built-in Functions"
268 The arithmetic functions are as follows:
272 \fB\fBcos\fR(\fIx\fR)\fR
275 Return cosine of \fIx\fR, where \fIx\fR is in radians. (In
276 \fB/usr/xpg4/bin/awk\fR only. See \fBnawk\fR(1).)
282 \fB\fBsin\fR(\fIx\fR)\fR
285 Return sine of \fIx\fR, where \fIx\fR is in radians. (In
286 \fB/usr/xpg4/bin/awk\fR only. See \fBnawk\fR(1).)
292 \fB\fBexp\fR(\fIx\fR)\fR
295 Return the exponential function of \fIx\fR.
301 \fB\fBlog\fR(\fIx\fR)\fR
304 Return the natural logarithm of \fIx\fR.
310 \fB\fBsqrt\fR(\fIx\fR)\fR
313 Return the square root of \fIx\fR.
319 \fB\fBint\fR(\fIx\fR)\fR
322 Truncate its argument to an integer. It is truncated toward \fB0\fR when
328 The string functions are as follows:
332 \fB\fBindex(\fR\fIs\fR\fB, \fR\fIt\fR\fB)\fR\fR
336 Return the position in string \fIs\fR where string \fIt\fR first occurs, or
337 \fB0\fR if it does not occur at all.
343 \fB\fBint(\fR\fIs\fR\fB)\fR\fR
347 truncates \fIs\fR to an integer value. If \fIs\fR is not specified, $0 is used.
353 \fB\fBlength(\fR\fIs\fR\fB)\fR\fR
357 Return the length of its argument taken as a string, or of the whole line if
358 there is no argument.
364 \fB\fBsplit(\fR\fIs\fR, \fIa\fR, \fIfs\fR\fB)\fR\fR
368 Split the string \fIs\fR into array elements \fIa\fR[\fI1\fR],
369 \fIa\fR[\fI2\fR], \|.\|.\|. \fIa\fR[\fIn\fR], and returns \fIn\fR. The
370 separation is done with the regular expression \fIfs\fR or with the field
371 separator \fBFS\fR if \fIfs\fR is not given.
377 \fB\fBsprintf(\fR\fIfmt\fR, \fIexpr\fR, \fIexpr\fR,\|.\|.\|.\|\fB)\fR\fR
381 Format the expressions according to the \fBprintf\fR(3C) format given by
382 \fIfmt\fR and returns the resulting string.
388 \fB\fBsubstr(\fR\fIs\fR, \fIm\fR, \fIn\fR\fB)\fR\fR
392 returns the \fIn\fR-character substring of \fIs\fR that begins at position
398 The input/output function is as follows:
405 Set \fB$0\fR to the next input record from the current input file.
406 \fBgetline\fR returns \fB1\fR for successful input, \fB0\fR for end of file,
407 and \fB\(mi1\fR for an error.
410 .SS "Large File Behavior"
413 See \fBlargefile\fR(5) for the description of the behavior of \fBawk\fR when
414 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
417 \fBExample 1 \fRPrinting Lines Longer Than 72 Characters
420 The following example is an \fBawk\fR script that can be executed by an \fBawk
421 -f examplescript\fR style command. It prints lines longer than seventy two
433 \fBExample 2 \fRPrinting Fields in Opposite Order
436 The following example is an \fBawk\fR script that can be executed by an \fBawk
437 -f examplescript\fR style command. It prints the first two fields in opposite
443 \fB{ print $2, $1 }\fR
449 \fBExample 3 \fRPrinting Fields in Opposite Order with the Input Fields
453 The following example is an \fBawk\fR script that can be executed by an \fBawk
454 -f examplescript\fR style command. It prints the first two input fields in
455 opposite order, separated by a comma, blanks or tabs:
460 \fBBEGIN { FS = ",[ \et]*|[ \et]+" }
467 \fBExample 4 \fRAdding Up the First Column, Printing the Sum and Average
470 The following example is an \fBawk\fR script that can be executed by an \fBawk
471 -f examplescript\fR style command. It adds up the first column, and prints the
478 END { print "sum is", s, " average is", s/NR }\fR
484 \fBExample 5 \fRPrinting Fields in Reverse Order
487 The following example is an \fBawk\fR script that can be executed by an \fBawk
488 -f examplescript\fR style command. It prints fields in reverse order:
493 \fB{ for (i = NF; i > 0; \(mi\(mii) print $i }\fR
499 \fBExample 6 \fRPrinting All lines Between \fBstart/stop\fR Pairs
502 The following example is an \fBawk\fR script that can be executed by an \fBawk
503 -f examplescript\fR style command. It prints all lines between start/stop
509 \fB/start/, /stop/\fR
515 \fBExample 7 \fRPrinting All Lines Whose First Field is Different from the
519 The following example is an \fBawk\fR script that can be executed by an \fBawk
520 -f examplescript\fR style command. It prints all lines whose first field is
521 different from the previous one.
526 \fB$1 != prev { print; prev = $1 }\fR
532 \fBExample 8 \fRPrinting a File and Filling in Page numbers
535 The following example is an \fBawk\fR script that can be executed by an \fBawk
536 -f examplescript\fR style command. It prints a file and fills in page numbers
542 \fB/Page/ { $2 = n++; }
549 \fBExample 9 \fRPrinting a File and Numbering Its Pages
552 Assuming this program is in a file named \fBprog\fR, the following example
553 prints the file \fBinput\fR numbering its pages starting at \fB5\fR:
558 example% \fBawk -f prog n=5 input\fR
563 .SH ENVIRONMENT VARIABLES
566 See \fBenviron\fR(5) for descriptions of the following environment variables
567 that affect the execution of \fBawk\fR: \fBLANG\fR, \fBLC_ALL\fR,
568 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, \fBNLSPATH\fR, and
573 \fB\fBLC_NUMERIC\fR\fR
576 Determine the radix character used when interpreting numeric input, performing
577 conversions between numeric and string values and formatting numeric output.
578 Regardless of locale, the period character (the decimal-point character of the
579 POSIX locale) is the decimal-point character recognized in processing \fBawk\fR
580 programs (including assignments in command-line arguments).
586 See \fBattributes\fR(5) for descriptions of the following attributes:
595 ATTRIBUTE TYPE ATTRIBUTE VALUE
600 .SS "/usr/xpg4/bin/awk"
608 ATTRIBUTE TYPE ATTRIBUTE VALUE
612 Interface Stability Standard
618 \fBegrep\fR(1), \fBgrep\fR(1), \fBnawk\fR(1), \fBsed\fR(1), \fBprintf\fR(3C),
619 \fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBstandards\fR(5)
623 Input white space is not preserved on output if fields are involved.
626 There are no explicit conversions between numbers and strings. To force an
627 expression to be treated as a number, add \fB0\fR to it. To force an expression
628 to be treated as a string, concatenate the null string (\fB""\fR) to it.