[BACK]Return to awk.1 CVS log [TXT][DIR] Up to [local] / src / usr.bin / awk

Annotation of src/usr.bin/awk/awk.1, Revision 1.18

1.18    ! jmc         1: .\"    $OpenBSD: awk.1,v 1.17 2003/12/16 11:18:37 jmc Exp $
1.7       aaron       2: .\" EX/EE is a Bd
1.11      jmc         3: .\"
                      4: .\" Copyright (C) Lucent Technologies 1997
                      5: .\" All Rights Reserved
1.12      jmc         6: .\"
1.11      jmc         7: .\" Permission to use, copy, modify, and distribute this software and
                      8: .\" its documentation for any purpose and without fee is hereby
                      9: .\" granted, provided that the above copyright notice appear in all
                     10: .\" copies and that both that the copyright notice and this
                     11: .\" permission notice and warranty disclaimer appear in supporting
                     12: .\" documentation, and that the name Lucent Technologies or any of
                     13: .\" its entities not be used in advertising or publicity pertaining
                     14: .\" to distribution of the software without specific, written prior
                     15: .\" permission.
1.12      jmc        16: .\"
1.11      jmc        17: .\" LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
                     18: .\" INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
                     19: .\" IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
                     20: .\" SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     21: .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
                     22: .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
                     23: .\" ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
                     24: .\" THIS SOFTWARE.
                     25: .\"
1.7       aaron      26: .Dd June 29, 1996
                     27: .Dt AWK 1
                     28: .Os
                     29: .Sh NAME
                     30: .Nm awk
                     31: .Nd pattern-directed scanning and processing language
                     32: .Sh SYNOPSIS
                     33: .Nm awk
1.16      jmc        34: .Op Fl safe
                     35: .Op Fl V
                     36: .Op Fl d Ns Op Ar n
1.7       aaron      37: .Op Fl F Ar fs
1.16      jmc        38: .Oo Fl v Ar var Ns =
                     39: .Ns Ar value Oc
1.18    ! jmc        40: .Op Ar prog | Fl f Ar progfile
1.7       aaron      41: .Ar
                     42: .Nm nawk
                     43: .Ar ...
                     44: .Sh DESCRIPTION
                     45: .Nm
1.1       tholo      46: scans each input
1.7       aaron      47: .Ar file
1.1       tholo      48: for lines that match any of a set of patterns specified literally in
1.7       aaron      49: .Ar prog
1.16      jmc        50: or in one or more files specified as
1.7       aaron      51: .Fl f Ar progfile .
1.16      jmc        52: With each pattern there can be an associated action that will be performed
1.1       tholo      53: when a line of a
1.7       aaron      54: .Ar file
1.1       tholo      55: matches the pattern.
                     56: Each line is matched against the
                     57: pattern portion of every pattern-action statement;
                     58: the associated action is performed for each matched pattern.
1.6       aaron      59: The file name
1.16      jmc        60: .Sq -
1.1       tholo      61: means the standard input.
                     62: Any
1.7       aaron      63: .Ar file
1.1       tholo      64: of the form
1.16      jmc        65: .Ar var Ns = Ns Ar value
1.1       tholo      66: is treated as an assignment, not a filename,
                     67: and is executed at the time it would have been opened if it were a filename.
1.16      jmc        68: .Pp
                     69: The options are as follows:
                     70: .Bl -tag -width Ds
                     71: .It Fl d Ns Op Ar n
                     72: Debug mode.
                     73: Set debug level to
                     74: .Ar n ,
                     75: or 1 if
                     76: .Ar n
                     77: is not specified.
                     78: A value greater than 1 causes
                     79: .Nm
                     80: to dump core on fatal errors.
                     81: .It Fl F Ar fs
                     82: Define the input field separator to be the regular expression
1.7       aaron      83: .Ar fs .
1.16      jmc        84: .It Fl f Ar filename
                     85: Read program code from the specified file
                     86: .Ar filename
                     87: instead of from the command line.
                     88: .It Fl safe
                     89: Disable file output
1.17      jmc        90: .Pf ( Ic print No > ,
                     91: .Ic print No >> ) ,
1.7       aaron      92: process creation
                     93: .Po
1.17      jmc        94: .Ar cmd | Ic getline ,
                     95: .Ic print No \&| ,
                     96: .Ic system
1.7       aaron      97: .Pc
                     98: and access to the environment
1.17      jmc        99: .Pf ( Va ENVIRON ;
1.18    ! jmc       100: see the section on variables below).
1.17      jmc       101: This is a first
1.16      jmc       102: .Pq and not very reliable
                    103: approximation to a
1.7       aaron     104: .Dq safe
                    105: version of
1.16      jmc       106: .Nm .
                    107: .It Fl V
                    108: Print the version number of
                    109: .Nm
                    110: to standard output and exit.
                    111: .It Fl v Ar var Ns = Ns Ar value
                    112: Assign
                    113: .Ar value
                    114: to variable
                    115: .Ar var
                    116: before
                    117: .Ar prog
                    118: is executed;
                    119: any number of
                    120: .Fl v
                    121: options may be present.
                    122: .El
1.7       aaron     123: .Pp
1.18    ! jmc       124: The input is normally made up of input lines
        !           125: .Pq records
        !           126: separated by newlines, or by the value of
        !           127: .Va RS .
        !           128: If
        !           129: .Va RS
        !           130: is null, then any number of blank lines are used as the record separator,
        !           131: and newlines are used as field separators
        !           132: (in addition to the value of
        !           133: .Va FS ) .
        !           134: This is convenient when working with multi-line records.
        !           135: .Pp
1.7       aaron     136: An input line is normally made up of fields separated by whitespace,
1.18    ! jmc       137: or by the regular expression
1.7       aaron     138: .Va FS .
1.1       tholo     139: The fields are denoted
1.7       aaron     140: .Va $1 , $2 , ... ,
                    141: while
                    142: .Va $0
1.1       tholo     143: refers to the entire line.
                    144: If
1.7       aaron     145: .Va FS
1.1       tholo     146: is null, the input line is split into one field per character.
1.7       aaron     147: .Pp
1.18    ! jmc       148: Normally, any number of blanks separate fields.
        !           149: In order to set the field separator to a single blank, use the
        !           150: .Fl F
        !           151: option with a value of
        !           152: .Sq [\ \&] .
        !           153: If a field separator of
        !           154: .Sq t
        !           155: is specified,
        !           156: .Nm
        !           157: treats it as if
        !           158: .Sq \et
        !           159: had been specified and uses
        !           160: .Aq TAB
        !           161: as the field separator.
        !           162: In order to use a literal
        !           163: .Sq t
        !           164: as the field separator, use the
        !           165: .Fl F
        !           166: option with a value of
        !           167: .Sq [t] .
        !           168: .Pp
1.1       tholo     169: A pattern-action statement has the form
1.7       aaron     170: .Pp
                    171: .D1 Ar pattern Ic \&{ Ar action Ic \&}
                    172: .Pp
1.6       aaron     173: A missing
1.7       aaron     174: .Ic \&{ Ar action Ic \&}
1.1       tholo     175: means print the line;
                    176: a missing pattern always matches.
                    177: Pattern-action statements are separated by newlines or semicolons.
1.7       aaron     178: .Pp
1.18    ! jmc       179: Newlines are permitted after a terminating statement or following a comma
        !           180: .Pq Sq ,\& ,
        !           181: an open brace
        !           182: .Pq Sq { ,
        !           183: a logical AND
        !           184: .Pq Sq && ,
        !           185: a logical OR
        !           186: .Pq Sq || ,
        !           187: after the
        !           188: .Sq do
        !           189: or
        !           190: .Sq else
        !           191: keywords,
        !           192: or after the closing parenthesis of an
        !           193: .Sq if ,
        !           194: .Sq for ,
        !           195: or
        !           196: .Sq while
        !           197: statement.
        !           198: Additionally, a backslash
        !           199: .Pq Sq \e
        !           200: can be used to escape a newline between tokens.
        !           201: .Pp
1.1       tholo     202: An action is a sequence of statements.
                    203: A statement can be one of the following:
1.7       aaron     204: .Bd -unfilled -offset indent
                    205: .Ic if ( Xo
                    206: .Ar expression ) statement \&
                    207: .Op Ic else Ar statement
                    208: .Xc
                    209: .Ic while ( Ar expression ) statement
                    210: .Ic for ( Xo
                    211: .Ar expression ; expression ; expression ) statement
                    212: .Xc
                    213: .Ic for ( Xo
                    214: .Ar var Ic in Ar array ) statement
                    215: .Xc
                    216: .Ic do Ar statement Ic while ( Ar expression )
                    217: .Ic break
                    218: .Ic continue
                    219: .Ic { Oo Ar statement ... Oc Ic \& }
                    220: .Ar expression Xo
                    221: .No "# commonly" \&
                    222: .Ar var Ic = Ar expression
                    223: .Xc
                    224: .Ic print Xo
                    225: .Op Ar expression-list
1.17      jmc       226: .Op > Ns Ar expression
1.7       aaron     227: .Xc
                    228: .Ic printf Ar format Xo
                    229: .Op Ar ... , expression-list
1.17      jmc       230: .Op > Ns Ar expression
1.7       aaron     231: .Xc
                    232: .Ic return Op Ar expression
                    233: .Ic next Xo
                    234: .No "# skip remaining patterns on this input line"
                    235: .Xc
                    236: .Ic nextfile Xo
                    237: .No "# skip rest of this file, open next, start at top"
                    238: .Xc
                    239: .Ic delete Ar array Ns Xo
                    240: .Ic \&[ Ns Ar expression Ns Ic \&]
                    241: .No \& "# delete an array element"
                    242: .Xc
                    243: .Ic delete Ar array Xo
                    244: .No "# delete all elements of array"
                    245: .Xc
                    246: .Ic exit Xo
                    247: .Op Ar expression
                    248: .No \& "# exit immediately; status is" Ar expression
                    249: .Xc
                    250: .Ed
                    251: .Pp
1.1       tholo     252: Statements are terminated by
                    253: semicolons, newlines or right braces.
                    254: An empty
1.7       aaron     255: .Ar expression-list
1.1       tholo     256: stands for
1.7       aaron     257: .Ar $0 .
                    258: String constants are quoted
                    259: .Li \&"" ,
1.1       tholo     260: with the usual C escapes recognized within.
                    261: Expressions take on string or numeric values as appropriate,
                    262: and are built using the operators
1.7       aaron     263: .Ic + \- * / % ^
                    264: (exponentiation), and concatenation (indicated by whitespace).
1.1       tholo     265: The operators
1.16      jmc       266: .Ic \&! ++ \-\- += \-= *= /= %= ^=
                    267: .Ic > >= < <= == != ?:
1.1       tholo     268: are also available in expressions.
                    269: Variables may be scalars, array elements
                    270: (denoted
1.7       aaron     271: .Li x[i] )
1.1       tholo     272: or fields.
                    273: Variables are initialized to the null string.
                    274: Array subscripts may be any string,
                    275: not necessarily numeric;
                    276: this allows for a form of associative memory.
                    277: Multiple subscripts such as
1.7       aaron     278: .Li [i,j,k]
1.1       tholo     279: are permitted; the constituents are concatenated,
                    280: separated by the value of
1.17      jmc       281: .Va SUBSEP
1.18    ! jmc       282: .Pq see the section on variables below ) .
1.7       aaron     283: .Pp
1.1       tholo     284: The
1.7       aaron     285: .Ic print
1.1       tholo     286: statement prints its arguments on the standard output
                    287: (or on a file if
1.17      jmc       288: .Pf > Ns Ar file
1.1       tholo     289: or
1.17      jmc       290: .Pf >> Ns Ar file
1.1       tholo     291: is present or on a pipe if
1.17      jmc       292: .Pf |\ \& Ar cmd
1.1       tholo     293: is present), separated by the current output field separator,
                    294: and terminated by the output record separator.
1.7       aaron     295: .Ar file
1.1       tholo     296: and
1.7       aaron     297: .Ar cmd
1.1       tholo     298: may be literal names or parenthesized expressions;
                    299: identical string values in different statements denote
                    300: the same open file.
                    301: The
1.7       aaron     302: .Ic printf
1.1       tholo     303: statement formats its expression list according to the format
                    304: (see
1.10      pvalchev  305: .Xr printf 3 ) .
1.18    ! jmc       306: .Pp
        !           307: Patterns are arbitrary Boolean combinations
        !           308: (with
        !           309: .Ic "\&! || &&" )
        !           310: of regular expressions and
        !           311: relational expressions.
        !           312: Regular expressions are as in
        !           313: .Xr egrep 1 .
        !           314: Isolated regular expressions
        !           315: in a pattern apply to the entire line.
        !           316: Regular expressions may also occur in
        !           317: relational expressions, using the operators
        !           318: .Ic ~
        !           319: and
        !           320: .Ic !~ .
        !           321: .Pf / Ns Ar re Ns /
        !           322: is a constant regular expression;
        !           323: any string (constant or variable) may be used
        !           324: as a regular expression, except in the position of an isolated regular expression
        !           325: in a pattern.
        !           326: .Pp
        !           327: A pattern may consist of two patterns separated by a comma;
        !           328: in this case, the action is performed for all lines
        !           329: from an occurrence of the first pattern
        !           330: through an occurrence of the second.
        !           331: .Pp
        !           332: A relational expression is one of the following:
        !           333: .Bd -unfilled -offset indent
        !           334: .Ar expression matchop regular-expression
        !           335: .Ar expression relop expression
        !           336: .Ar expression Ic in Ar array-name
        !           337: .Ic \&( Ns Xo
        !           338: .Ar expr , expr , \&... Ns Ic \&) in
        !           339: .Ar \& array-name
        !           340: .Xc
        !           341: .Ed
        !           342: .Pp
        !           343: where a
        !           344: .Ar relop
        !           345: is any of the six relational operators in C, and a
        !           346: .Ar matchop
        !           347: is either
        !           348: .Ic ~
        !           349: (matches)
        !           350: or
        !           351: .Ic !~
        !           352: (does not match).
        !           353: A conditional is an arithmetic expression,
        !           354: a relational expression,
        !           355: or a Boolean combination
        !           356: of these.
        !           357: .Pp
        !           358: The special patterns
        !           359: .Ic BEGIN
        !           360: and
        !           361: .Ic END
        !           362: may be used to capture control before the first input line is read
        !           363: and after the last.
        !           364: .Ic BEGIN
        !           365: and
        !           366: .Ic END
        !           367: do not combine with other patterns.
        !           368: .Pp
        !           369: Variable names with special meanings:
        !           370: .Pp
        !           371: .Bl -tag -width "FILENAME" -compact
        !           372: .It Va ARGC
        !           373: Argument count, assignable.
        !           374: .It Va ARGV
        !           375: Argument array, assignable;
        !           376: non-null members are taken as filenames.
        !           377: .It Va CONVFMT
        !           378: Conversion format when converting numbers
        !           379: (default
        !           380: .Qq Li %.6g ) .
        !           381: .It Va ENVIRON
        !           382: Array of environment variables; subscripts are names.
        !           383: .It Va FILENAME
        !           384: The name of the current input file.
        !           385: .It Va FNR
        !           386: Ordinal number of the current record in the current file.
        !           387: .It Va FS
        !           388: Regular expression used to separate fields; also settable
        !           389: by option
        !           390: .Fl F Ar fs .
        !           391: .It Va NF
        !           392: Number of fields in the current record.
        !           393: .Va $NF
        !           394: can be used to obtain the value of the last field in the current record.
        !           395: .It Va NR
        !           396: Ordinal number of the current record.
        !           397: .It Va OFMT
        !           398: Output format for numbers (default
        !           399: .Qq Li %.6g ) .
        !           400: .It Va OFS
        !           401: Output field separator (default blank).
        !           402: .It Va ORS
        !           403: Output record separator (default newline).
        !           404: .It Va RLENGTH
        !           405: The length of the string matched by the
        !           406: .Fn match
        !           407: function.
        !           408: .It Va RS
        !           409: Input record separator (default newline).
        !           410: .It Va RSTART
        !           411: The starting position of the string matched by the
        !           412: .Fn match
        !           413: function.
        !           414: .It Va SUBSEP
        !           415: Separates multiple subscripts (default 034).
        !           416: .El
1.17      jmc       417: .Sh FUNCTIONS
                    418: The awk language has a variety of built-in functions:
                    419: arithmetic, string, input/output and general.
                    420: .Ss Arithmetic Functions
                    421: .Bl -tag -width "atan2(y, x)"
                    422: .It Fn atan2 y x
                    423: Return the arctangent of
                    424: .Fa y Ns / Ns Fa x
                    425: in radians.
                    426: .It Fn cos x
                    427: Return the cosine of
                    428: .Fa x ,
                    429: where
                    430: .Fa x
                    431: is in radians.
                    432: .It Fn exp x
                    433: Return the exponential of
                    434: .Fa x .
                    435: .It Fn int x
                    436: Return
                    437: .Fa x
                    438: truncated to an integer value.
                    439: .It Fn log x
                    440: Return the natural logarithm of
                    441: .Fa x .
1.7       aaron     442: .It Fn rand
1.17      jmc       443: Return a random number,
                    444: .Fa n ,
                    445: such that
                    446: .Sm off
                    447: .Pf 0 \*(Le Fa n No \*(Lt 1 .
                    448: .Sm on
                    449: .It Fn sin x
                    450: Return the sine of
                    451: .Fa x ,
                    452: where
                    453: .Fa x
                    454: is in radians.
                    455: .It Fn sqrt x
                    456: Return the square root of
                    457: .Fa x .
                    458: .It Fn srand expr
1.16      jmc       459: Sets seed for
1.7       aaron     460: .Fn rand
1.17      jmc       461: to
                    462: .Fa expr
1.1       tholo     463: and returns the previous seed.
1.17      jmc       464: If
                    465: .Fa expr
                    466: is omitted, the time of day is used instead.
                    467: .El
                    468: .Ss String Functions
                    469: .Bl -tag -width "split(s, a, fs)"
                    470: .It Fn gsub r t s
                    471: The same as
                    472: .Fn sub
                    473: except that all occurrences of the regular expression are replaced.
                    474: .Fn gsub
                    475: returns the number of replacements.
1.7       aaron     476: .It Fn index s t
1.16      jmc       477: The position in
1.7       aaron     478: .Fa s
1.1       tholo     479: where the string
1.7       aaron     480: .Fa t
1.1       tholo     481: occurs, or 0 if it does not.
1.17      jmc       482: .It Fn length s
                    483: The length of
                    484: .Fa s
                    485: taken as a string,
                    486: or of
                    487: .Va $0
                    488: if no argument is given.
1.7       aaron     489: .It Fn match s r
1.16      jmc       490: The position in
1.7       aaron     491: .Fa s
1.1       tholo     492: where the regular expression
1.7       aaron     493: .Fa r
1.1       tholo     494: occurs, or 0 if it does not.
1.17      jmc       495: The variable
1.7       aaron     496: .Va RSTART
1.17      jmc       497: is set to the starting position of the matched string
                    498: .Pq which is the same as the returned value
                    499: or zero if no match is found.
                    500: The variable
1.7       aaron     501: .Va RLENGTH
1.17      jmc       502: is set to the length of the matched string,
                    503: or \-1 if no match is found.
1.7       aaron     504: .It Fn split s a fs
1.16      jmc       505: Splits the string
1.7       aaron     506: .Fa s
1.1       tholo     507: into array elements
1.7       aaron     508: .Va a[1] , a[2] , ... , a[n]
1.1       tholo     509: and returns
1.7       aaron     510: .Va n .
1.1       tholo     511: The separation is done with the regular expression
1.7       aaron     512: .Ar fs
1.1       tholo     513: or with the field separator
1.7       aaron     514: .Va FS
1.1       tholo     515: if
1.7       aaron     516: .Ar fs
1.1       tholo     517: is not given.
                    518: An empty string as field separator splits the string
                    519: into one array element per character.
1.17      jmc       520: .It Fn sprintf fmt expr ...
                    521: The string resulting from formatting
                    522: .Fa expr , ...
                    523: according to the
                    524: .Xr printf 3
                    525: format
                    526: .Fa fmt .
1.7       aaron     527: .It Fn sub r t s
1.16      jmc       528: Substitutes
1.7       aaron     529: .Fa t
1.1       tholo     530: for the first occurrence of the regular expression
1.7       aaron     531: .Fa r
1.1       tholo     532: in the string
1.7       aaron     533: .Fa s .
1.1       tholo     534: If
1.7       aaron     535: .Fa s
1.1       tholo     536: is not given,
1.7       aaron     537: .Va $0
1.1       tholo     538: is used.
1.17      jmc       539: An ampersand
                    540: .Pq Sq &
                    541: in
                    542: .Fa t
                    543: is replaced in string
                    544: .Fa s
                    545: with regular expression
                    546: .Fa r .
                    547: A literal ampersand can be specified by preceding it with two backslashes
                    548: .Pq Sq \e\e .
                    549: A literal backslash can be specified by preceding it with another backslash
                    550: .Pq Sq \e\e .
1.7       aaron     551: .Fn sub
1.17      jmc       552: returns the number of replacements.
                    553: .It Fn substr s m n
                    554: Return at most the
                    555: .Fa n Ns -character
                    556: substring of
                    557: .Fa s
                    558: that begins at position
                    559: .Fa m
                    560: counted from 1.
                    561: If
                    562: .Fa n
                    563: is omitted, or if
                    564: .Fa n
                    565: specifies more characters than are left in the string,
                    566: the length of the substring is limited by the length of
                    567: .Fa s .
1.7       aaron     568: .It Fn tolower str
1.16      jmc       569: Returns a copy of
1.7       aaron     570: .Fa str
1.1       tholo     571: with all upper-case characters translated to their
                    572: corresponding lower-case equivalents.
1.7       aaron     573: .It Fn toupper str
1.16      jmc       574: Returns a copy of
1.7       aaron     575: .Fa str
1.1       tholo     576: with all lower-case characters translated to their
                    577: corresponding upper-case equivalents.
1.7       aaron     578: .El
1.17      jmc       579: .Ss Input/Output and General Functions
                    580: .Bl -tag -width "getline [var] < file"
                    581: .It Fn close expr
                    582: Closes the file or pipe
                    583: .Fa expr .
                    584: .Fa expr
                    585: should match the string that was used to open the file or pipe.
                    586: .It Ar cmd | Ic getline Op Va var
                    587: Read a record of input from a stream piped from the output of
                    588: .Ar cmd .
                    589: If
                    590: .Va var
                    591: is omitted, the variables
                    592: .Va $0
                    593: and
                    594: .Va NF
                    595: are set.
                    596: Otherwise
                    597: .Va var
                    598: is set.
                    599: If the stream is not open, it is opened.
                    600: As long as the stream remains open, subsequent calls
                    601: will read subsequent records from the stream.
                    602: The stream remains open until explicitly closed with a call to
                    603: .Fn close .
                    604: .It Fn fflush expr
                    605: Flushes any buffered output for the file or pipe
                    606: .Fa expr .
                    607: .Fa expr
                    608: should match the string that was used to open the file or pipe.
                    609: .It Ic getline
                    610: Sets
                    611: .Va $0
                    612: to the next input record from the current input file.
                    613: This form of
                    614: .Ic getline
                    615: sets the variables
                    616: .Va NF ,
                    617: .Va NR ,
                    618: and
                    619: .Va FNR .
1.7       aaron     620: .Ic getline
1.17      jmc       621: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
                    622: .It Ic getline Va var
                    623: Sets
1.7       aaron     624: .Va $0
1.17      jmc       625: to variable
                    626: .Va var .
                    627: This form of
                    628: .Ic getline
                    629: sets the variables
                    630: .Va NR
                    631: and
                    632: .Va FNR .
                    633: .Ic getline
                    634: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
                    635: .It Xo
                    636: .Ic getline Op Va var
                    637: .Pf \ \&< Ar file
                    638: .Xc
                    639: Sets
1.7       aaron     640: .Va $0
1.1       tholo     641: to the next record from
1.7       aaron     642: .Ar file .
1.17      jmc       643: If
                    644: .Va var
                    645: is omitted, the variables
                    646: .Va $0
                    647: and
                    648: .Va NF
                    649: are set.
                    650: Otherwise
                    651: .Va var
                    652: is set.
                    653: If
                    654: .Ar file
                    655: is not open, it is opened.
                    656: As long as the stream remains open, subsequent calls will read subsequent
                    657: records from
                    658: .Ar file .
                    659: .Ar file
                    660: remains open until explicitly closed with a call to
                    661: .Fn close .
                    662: .It Fn system cmd
                    663: Executes
                    664: .Fa cmd
                    665: and returns its exit status.
                    666: .El
                    667: .Pp
                    668: Functions may be defined (at the position of a pattern-action statement)
                    669: thusly:
                    670: .Pp
                    671: .Dl function foo(a, b, c) { ...; return x }
1.7       aaron     672: .Pp
1.17      jmc       673: Parameters are passed by value if scalar, and by reference if array name;
                    674: functions may be called recursively.
                    675: Parameters are local to the function; all other variables are global.
                    676: Thus local variables may be created by providing excess parameters in
                    677: the function definition.
1.7       aaron     678: .Sh EXAMPLES
1.16      jmc       679: Print lines longer than 72 characters:
                    680: .Pp
1.7       aaron     681: .Dl length($0) > 72
1.16      jmc       682: .Pp
                    683: Print first two fields in opposite order:
1.7       aaron     684: .Pp
                    685: .Dl { print $2, $1 }
1.16      jmc       686: .Pp
                    687: Same, with input fields separated by comma and/or blanks and tabs:
1.7       aaron     688: .Bd -literal -offset indent
1.1       tholo     689: BEGIN { FS = ",[ \et]*|[ \et]+" }
                    690:       { print $2, $1 }
1.7       aaron     691: .Ed
1.16      jmc       692: .Pp
                    693: Add up first column, print sum and average:
1.7       aaron     694: .Bd -literal -offset indent
                    695: { s += $1 }
                    696: END { print "sum is", s, " average is", s/NR }
                    697: .Ed
1.16      jmc       698: .Pp
                    699: Print all lines between start/stop pairs:
1.7       aaron     700: .Pp
                    701: .Dl /start/, /stop/
1.16      jmc       702: .Pp
                    703: Simulate echo(1):
1.7       aaron     704: .Bd -literal -offset indent
                    705: BEGIN { # Simulate echo(1)
                    706:         for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
                    707:         printf "\en"
                    708:         exit }
                    709: .Ed
                    710: .Sh SEE ALSO
1.16      jmc       711: .Xr egrep 1 ,
1.7       aaron     712: .Xr lex 1 ,
1.16      jmc       713: .Xr sed 1 ,
                    714: .Xr printf 3
1.7       aaron     715: .Rs
                    716: .%A A. V. Aho
                    717: .%A B. W. Kernighan
                    718: .%A P. J. Weinberger
                    719: .%T The AWK Programming Language
                    720: .%I Addison-Wesley
                    721: .%D 1988
                    722: .%O ISBN 0-201-07981-X
                    723: .Re
1.8       aaron     724: .Sh HISTORY
1.13      millert   725: An
1.8       aaron     726: .Nm
1.13      millert   727: utility appeared in
                    728: .At v7 .
1.7       aaron     729: .Sh BUGS
1.1       tholo     730: There are no explicit conversions between numbers and strings.
                    731: To force an expression to be treated as a number add 0 to it;
                    732: to force it to be treated as a string concatenate
1.7       aaron     733: .Li \&""
                    734: to it.
                    735: .Pp
1.1       tholo     736: The scope rules for variables in functions are a botch;
                    737: the syntax is worse.