[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.36

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