.\" $OpenBSD: awk.1,v 1.70 2024/08/11 18:24:43 schwarze Exp $ .\" .\" Copyright (C) Lucent Technologies 1997 .\" All Rights Reserved .\" .\" Permission to use, copy, modify, and distribute this software and .\" its documentation for any purpose and without fee is hereby .\" granted, provided that the above copyright notice appear in all .\" copies and that both that the copyright notice and this .\" permission notice and warranty disclaimer appear in supporting .\" documentation, and that the name Lucent Technologies or any of .\" its entities not be used in advertising or publicity pertaining .\" to distribution of the software without specific, written prior .\" permission. .\" .\" LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, .\" INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. .\" IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY .\" SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, .\" ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF .\" THIS SOFTWARE. .\" .Dd $Mdocdate: August 11 2024 $ .Dt AWK 1 .Os .Sh NAME .Nm awk .Nd pattern-directed scanning and processing language .Sh SYNOPSIS .Nm awk .Op Fl safe .Op Fl V .Op Fl d Ns Op Ar n .Op Fl F Ar fs | Fl -csv .Op Fl v Ar var Ns = Ns Ar value .Op Ar prog | Fl f Ar progfile .Ar .Sh DESCRIPTION .Nm scans each input .Ar file for lines that match any of a set of patterns specified literally in .Ar prog or in one or more files specified as .Fl f Ar progfile . With each pattern there can be an associated action that will be performed when a line of a .Ar file matches the pattern. Each line is matched against the pattern portion of every pattern-action statement; the associated action is performed for each matched pattern. The file name .Sq - means the standard input. Any .Ar file of the form .Ar var Ns = Ns Ar value is treated as an assignment, not a filename, and is executed at the time it would have been opened if it were a filename. .Pp The options are as follows: .Bl -tag -width "-safe " .It Fl -csv Process records using the (more or less) standard comma-separated values .Pq CSV format instead of the input field separator. When the .Fl -csv option is specified, attempts to change the input field separator or record separator are ignored. .It Fl d Ns Op Ar n Debug mode. Set debug level to .Ar n , or 1 if .Ar n is not specified. A value greater than 1 causes .Nm to dump core on fatal errors. .It Fl F Ar fs Define the input field separator to be the regular expression .Ar fs . .It Fl f Ar progfile Read program code from the specified file .Ar progfile instead of from the command line. .It Fl safe Disable file output .Pf ( Ic print No > , .Ic print No >> ) , process creation .Po .Ar cmd | Ic getline , .Ic print | , .Ic system .Pc and access to the environment .Pf ( Va ENVIRON ; see the section on variables below). This is a first .Pq and not very reliable approximation to a .Dq safe version of .Nm . .It Fl V Print the version number of .Nm to standard output and exit. .It Fl v Ar var Ns = Ns Ar value Assign .Ar value to variable .Ar var before .Ar prog is executed; any number of .Fl v options may be present. .El .Pp The input is normally made up of input lines .Pq records separated by newlines, or by the value of .Va RS . If .Va RS is null, then any number of blank lines are used as the record separator, and newlines are used as field separators (in addition to the value of .Va FS ) . This is convenient when working with multi-line records. .Pp An input line is normally made up of fields separated by whitespace, or by the value of the field separator .Va FS at the time the line is read. The fields are denoted .Va $1 , $2 , ... , while .Va $0 refers to the entire line. .Va FS may be set to either a single character or a regular expression. As a special case, if .Va FS is a single space .Pq the default , fields will be split by one or more whitespace characters. If .Va FS is null, the input line is split into one field per character. .Pp Normally, any number of blanks separate fields. In order to set the field separator to a single blank, use the .Fl F option with a value of .Sq [\ \&] . If a field separator of .Sq t is specified, .Nm treats it as if .Sq \et had been specified and uses .Aq TAB as the field separator. In order to use a literal .Sq t as the field separator, use the .Fl F option with a value of .Sq [t] . The field separator is usually set via the .Fl F option or from inside a .Ic BEGIN block so that it takes effect before the input is read. .Pp A pattern-action statement has the form: .Pp .D1 Ar pattern Ic \&{ Ar action Ic \&} .Pp A missing .Ic \&{ Ar action Ic \&} means print the line; a missing pattern always matches. Pattern-action statements are separated by newlines or semicolons. .Pp Newlines are permitted after a terminating statement or following a comma .Pq Sq ,\& , an open brace .Pq Sq { , a logical AND .Pq Sq && , a logical OR .Pq Sq || , after the .Sq do or .Sq else keywords, or after the closing parenthesis of an .Sq if , .Sq for , or .Sq while statement. Additionally, a backslash .Pq Sq \e can be used to escape a newline between tokens. .Pp An action is a sequence of statements. A statement can be one of the following: .Pp .Bl -tag -width Ds -offset indent -compact .It Ic if Ar ( expression ) Ar statement Op Ic else Ar statement .It Ic while Ar ( expression ) Ar statement .It Ic for Ar ( expression ; expression ; expression ) statement .It Ic for Ar ( var Ic in Ar array ) statement .It Ic do Ar statement Ic while Ar ( expression ) .It Ic break .It Ic continue .It Xo Ic { .Op Ar statement ... .Ic } .Xc .It Xo Ar expression .No # commonly .Ar var No = Ar expression .Xc .It Xo Ic print .Op Ar expression-list .Op > Ns Ar expression .Xc .It Xo Ic printf Ar format .Op Ar ... , expression-list .Op > Ns Ar expression .Xc .It Ic return Op Ar expression .It Xo Ic next .No # skip remaining patterns on this input line .Xc .It Xo Ic nextfile .No # skip rest of this file, open next, start at top .Xc .It Xo Ic delete .Sm off .Ar array Ic \&[ Ar expression Ic \&] .Sm on .No # delete an array element .Xc .It Xo Ic delete Ar array .No # delete all elements of array .Xc .It Xo Ic exit .Op Ar expression .No # exit processing, and perform .Ic END processing; status is .Ar expression .Xc .El .Pp Statements are terminated by semicolons, newlines or right braces. An empty .Ar expression-list stands for .Ar $0 . String constants are quoted .Li \&"" , with the usual C escapes recognized within (see .Xr printf 1 for a complete list of these). Expressions take on string or numeric values as appropriate, and are built using the operators .Ic + \- * / % ^ .Pq exponentiation , and concatenation .Pq indicated by whitespace . The operators .Ic \&! ++ \-\- += \-= *= /= %= ^= .Ic > >= < <= == != ?\&: are also available in expressions. Variables may be scalars, array elements (denoted .Li x[i] ) or fields. Variables are initialized to the null string. Array subscripts may be any string, not necessarily numeric; this allows for a form of associative memory. Multiple subscripts such as .Li [i,j,k] are permitted; the constituents are concatenated, separated by the value of .Va SUBSEP .Pq see the section on variables below . .Pp The .Ic print statement prints its arguments on the standard output (or on a file if .Pf >\ \& Ar file or .Pf >>\ \& Ar file is present or on a pipe if .Pf |\ \& Ar cmd is present), separated by the current output field separator, and terminated by the output record separator. .Ar file and .Ar cmd may be literal names or parenthesized expressions; identical string values in different statements denote the same open file. The .Ic printf statement formats its expression list according to the .Ar format (see .Xr printf 1 ) . .Pp Patterns are arbitrary Boolean combinations (with .Ic "\&! || &&" ) of regular expressions and relational expressions. .Nm supports extended regular expressions .Pq EREs . See .Xr re_format 7 for more information on regular expressions. Isolated regular expressions in a pattern apply to the entire line. Regular expressions may also occur in relational expressions, using the operators .Ic ~ and .Ic !~ . .Pf / Ar re Ns / is a constant regular expression; any string (constant or variable) may be used as a regular expression, except in the position of an isolated regular expression in a pattern. .Pp A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines from an occurrence of the first pattern through an occurrence of the second. .Pp A relational expression is one of the following: .Pp .Bl -tag -width Ds -offset indent -compact .It Ar expression matchop regular-expression .It Ar expression relop expression .It Ar expression Ic in Ar array-name .It Xo Ic \&( Ns .Ar expr , expr , \&... Ns Ic \&) in .Ar array-name .Xc .El .Pp where a .Ar relop is any of the six relational operators in C, and a .Ar matchop is either .Ic ~ (matches) or .Ic !~ (does not match). A conditional is an arithmetic expression, a relational expression, or a Boolean combination of these. .Pp The special pattern .Ic BEGIN may be used to capture control before the first input line is read. The special pattern .Ic END may be used to capture control after processing is finished. .Ic BEGIN and .Ic END do not combine with other patterns. They may appear multiple times in a program and execute in the order they are read by .Nm . .Pp Variable names with special meanings: .Pp .Bl -tag -width "FILENAME " -compact .It Va ARGC Argument count, assignable. .It Va ARGV Argument array, assignable; non-null members are taken as filenames. .It Va CONVFMT Conversion format when converting numbers (default .Qq Li %.6g ) . .It Va ENVIRON Array of environment variables; subscripts are names. .It Va FILENAME The name of the current input file. .It Va FNR Ordinal number of the current record in the current file. .It Va FS Regular expression used to separate fields (default whitespace); also settable by option .Fl F Ar fs . .It Va NF Number of fields in the current record. .Va $NF can be used to obtain the value of the last field in the current record. .It Va NR Ordinal number of the current record. .It Va OFMT Output format for numbers (default .Qq Li %.6g ) . .It Va OFS Output field separator (default blank). .It Va ORS Output record separator (default newline). .It Va RLENGTH The length of the string matched by the .Fn match function. .It Va RS Input record separator (default newline). If empty, blank lines separate records. If more than one character long, .Va RS is treated as a regular expression, and records are separated by text matching the expression. .It Va RSTART The starting position of the string matched by the .Fn match function. .It Va SUBSEP Separates multiple subscripts (default 034). .El .Sh FUNCTIONS The awk language has a variety of built-in functions: arithmetic, string, input/output, general, and bit-operation. .Pp Functions may be defined (at the position of a pattern-action statement) thusly: .Pp .Dl function foo(a, b, c) { ...; return x } .Pp Parameters are passed by value if scalar, and by reference if array name; functions may be called recursively. Parameters are local to the function; all other variables are global. Thus local variables may be created by providing excess parameters in the function definition. .Ss Arithmetic Functions .Bl -tag -width "atan2(y, x)" .It Fn atan2 y x Return the arctangent of .Fa y Ns / Ns Fa x in radians. .It Fn cos x Return the cosine of .Fa x , where .Fa x is in radians. .It Fn exp x Return the exponential of .Fa x . .It Fn int x Return .Fa x truncated to an integer value. .It Fn log x Return the natural logarithm of .Fa x . .It Fn rand Return a random number, .Fa n , such that .Sm off .Pf 0 \*(Le Fa n No \*(Lt 1 . .Sm on Random numbers are non-deterministic unless a seed is explicitly set with .Fn srand . .It Fn sin x Return the sine of .Fa x , where .Fa x is in radians. .It Fn sqrt x Return the square root of .Fa x . .It Fn srand expr Sets seed for .Fn rand to .Fa expr and returns the previous seed. If .Fa expr is omitted, .Fn rand will return non-deterministic random numbers. .El .Ss String Functions .Bl -tag -width "split(s, a, fs)" .It Fn gensub r s h [t] Search the target string .Ar t for matches of the regular expression .Ar r . If .Ar h is a string beginning with .Ic g or .Ic G , then replace all matches of .Ar r with .Ar s . Otherwise, .Ar h is a number indicating which match of .Ar r to replace. If no .Ar t is supplied, .Va $0 is used instead. .\"Within the replacement text .\".Ar s , .\"the sequence .\".Ar \en , .\"where .\".Ar n .\"is a digit from 1 to 9, may be used to indicate just the text that .\"matched the .\".Ar n Ap th .\"parenthesized subexpression. .\"The sequence .\".Ic \e0 .\"represents the entire text, as does the character .\".Ic & . Unlike .Fn sub and .Fn gsub , the modified string is returned as the result of the function, and the original target is .Em not changed. Note that .Ar \en sequences within the replacement string .Ar s , as supported by GNU .Nm , are .Em not supported at this time. .It Fn gsub r t s The same as .Fn sub except that all occurrences of the regular expression are replaced. .Fn gsub returns the number of replacements. .It Fn index s t The position in .Fa s where the string .Fa t occurs, or 0 if it does not. .It Fn length s The length of .Fa s taken as a string, number of elements in an array for an array argument, or length of .Va $0 if no argument is given. .It Fn match s r The position in .Fa s where the regular expression .Fa r occurs, or 0 if it does not. The variable .Va RSTART is set to the starting position of the matched string .Pq which is the same as the returned value or zero if no match is found. The variable .Va RLENGTH is set to the length of the matched string, or \-1 if no match is found. .It Fn split s a fs Splits the string .Fa s into array elements .Va a[1] , a[2] , ... , a[n] and returns .Va n . The separation is done with the regular expression .Ar fs or with the field separator .Va FS if .Ar fs is not given. An empty string as field separator splits the string into one array element per character. .It Fn sprintf fmt expr ... The string resulting from formatting .Fa expr , ... according to the .Xr printf 1 format .Fa fmt . .It Fn sub r t s Substitutes .Fa t for the first occurrence of the regular expression .Fa r in the string .Fa s . If .Fa s is not given, .Va $0 is used. An ampersand .Pq Sq & in .Fa t is replaced in string .Fa s with regular expression .Fa r . A literal ampersand can be specified by preceding it with two backslashes .Pq Sq \e\e . A literal backslash can be specified by preceding it with another backslash .Pq Sq \e\e . .Fn sub returns the number of replacements. .It Fn substr s m n Return at most the .Fa n Ns -character substring of .Fa s that begins at position .Fa m counted from 1. If .Fa n is omitted, or if .Fa n specifies more characters than are left in the string, the length of the substring is limited by the length of .Fa s . .It Fn tolower str Returns a copy of .Fa str with all upper-case characters translated to their corresponding lower-case equivalents. .It Fn toupper str Returns a copy of .Fa str with all lower-case characters translated to their corresponding upper-case equivalents. .El .Ss Time Functions This version of .Nm provides the following functions for obtaining and formatting time stamps. .Bl -tag -width indent .It Fn mktime datespec Converts .Fa datespec into a timestamp in the same form as a value returned by .Fn systime . The .Fa datespec is a string composed of six or seven numbers separated by whitespace: .Bd -literal -offset indent YYYY MM DD HH MM SS [DST] .Ed .Pp The fields in .Fa datespec are as follows: .Bl -tag -width "YYYY" .It YYYY Year: a four-digit year, including the century. .It MM Month: a number from 1 to 12. .It DD Day: a number from 1 to 31. .It HH Hour: a number from 0 to 23. .It MM Minute: a number from 0 to 59. .It SS Second: a number from 0 to 60 (permitting a leap second). .It DST Daylight Saving Time: a positive or zero value indicates that DST is or is not in effect. If DST is not specified, or is negative, .Fn mktime will attempt to determine the correct value. .El .It Fn strftime "[format [, timestamp]]" Formats .Ar timestamp according to the string .Ar format . The format string may contain any of the conversion specifications described in the .Xr strftime 3 manual page, as well as any arbitrary text. The .Ar timestamp must be in the same form as a value returned by .Fn mktime and .Fn systime . If .Ar timestamp is not specified, the current time is used. If .Ar format is not specified, a default format equivalent to the output of .Xr date 1 is used. .It Fn systime Returns the value of time in seconds since 0 hours, 0 minutes, 0 seconds, January 1, 1970, Coordinated Universal Time (UTC). .El .Ss Input/Output and General Functions .Bl -tag -width "getline [var] < file" .It Fn close expr Closes the file or pipe .Fa expr . .Fa expr should match the string that was used to open the file or pipe. .It Ar cmd | Ic getline Op Va var Read a record of input from a stream piped from the output of .Ar cmd . If .Va var is omitted, the variables .Va $0 and .Va NF are set. Otherwise .Va var is set. If the stream is not open, it is opened. As long as the stream remains open, subsequent calls will read subsequent records from the stream. The stream remains open until explicitly closed with a call to .Fn close . .Ic getline returns 1 for a successful input, 0 for end of file, and \-1 for an error. .It Fn fflush [expr] Flushes any buffered output for the file or pipe .Fa expr , or all open files or pipes if .Fa expr is omitted. .Fa expr should match the string that was used to open the file or pipe. .It Ic getline Sets .Va $0 to the next input record from the current input file. This form of .Ic getline sets the variables .Va NF , .Va NR , and .Va FNR . .Ic getline returns 1 for a successful input, 0 for end of file, and \-1 for an error. .It Ic getline Va var Sets .Va $0 to variable .Va var . This form of .Ic getline sets the variables .Va NR and .Va FNR . .Ic getline returns 1 for a successful input, 0 for end of file, and \-1 for an error. .It Xo .Ic getline Op Va var .Pf <\ \& Ar file .Xc Sets .Va $0 to the next record from .Ar file . If .Va var is omitted, the variables .Va $0 and .Va NF are set. Otherwise .Va var is set. If .Ar file is not open, it is opened. As long as the stream remains open, subsequent calls will read subsequent records from .Ar file . .Ar file remains open until explicitly closed with a call to .Fn close . .It Fn system cmd Executes .Fa cmd and returns its exit status. This will be \-1 upon error, .Ar cmd Ns 's exit status upon a normal exit, 256 + .Em sig if .Fa cmd was terminated by a signal, where .Em sig is the number of the signal, or 512 + .Em sig if there was a core dump. .El .Ss Bit-Operation Functions .Bl -tag -width "lshift(a, b)" .It Fn compl x Returns the bitwise complement of integer argument x. .It Fn and x y Performs a bitwise AND on integer arguments x and y. .It Fn or x y Performs a bitwise OR on integer arguments x and y. .It Fn xor x y Performs a bitwise Exclusive-OR on integer arguments x and y. .It Fn lshift x n Returns integer argument x shifted by n bits to the left. .It Fn rshift x n Returns integer argument x shifted by n bits to the right. .El .Sh ENVIRONMENT The following environment variables affect the execution of .Nm : .Bl -tag -width POSIXLY_CORRECT .It Ev LC_CTYPE The character encoding .Xr locale 1 . It decides which byte sequences form characters, which characters are letters, and how letters are mapped from lower to upper case and vice versa. If unset or set to .Qq C , .Qq POSIX , or an unsupported value, each byte is treated as a character, and non-ASCII bytes are not regarded as letters. .It Ev POSIXLY_CORRECT When set, behave in accordance with the standard, even when it conflicts with historical behavior. .El .Sh EXIT STATUS .Ex -std awk .Pp But note that the .Ic exit expression can modify the exit status. .Sh EXAMPLES Print lines longer than 72 characters: .Pp .Dl length($0) > 72 .Pp Print first two fields in opposite order: .Pp .Dl { print $2, $1 } .Pp Same, with input fields separated by comma and/or spaces and tabs: .Bd -literal -offset indent BEGIN { FS = ",[ \et]*|[ \et]+" } { print $2, $1 } .Ed .Pp Add up first column, print sum and average: .Bd -literal -offset indent { s += $1 } END { print "sum is", s, " average is", s/NR } .Ed .Pp Print all lines between start/stop pairs: .Pp .Dl /start/, /stop/ .Pp Simulate .Xr echo 1 : .Bd -literal -offset indent BEGIN { # Simulate echo(1) for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i] printf "\en" exit } .Ed .Pp Print an error message to standard error: .Bd -literal -offset indent { print "error!" > "/dev/stderr" } .Ed .Sh UNUSUAL FLOATING-POINT VALUES .Nm was designed before IEEE 754 arithmetic defined Not-A-Number (NaN) and Infinity values, which are supported by all modern floating-point hardware. .Pp Because .Nm uses .Xr strtod 3 and .Xr atof 3 to convert string values to double-precision floating-point values, modern C libraries also convert strings starting with .Dv inf and .Dv nan into infinity and NaN values respectively. This led to strange results, with something like this: .Pp .Li echo nancy | awk '{ print $1 + 0 }' .Pp printing .Dv nan instead of zero. .Pp .Nm now follows GNU .Nm , and prefilters string values before attempting to convert them to numbers, as follows: .Bl -tag -width Ds .It Hexadecimal values Hexadecimal values (allowed since C99) convert to zero, as they did prior to C99. .It NaN values The two strings .Dq +NAN and .Dq -NAN (case independent) convert to NaN. No others do. (NaNs can have signs.) .It Infinity values The two strings .Dq +INF and .Dq -INF (case independent) convert to positive and negative infinity, respectively. No others do. .El .Sh SEE ALSO .Xr cut 1 , .Xr date 1 , .Xr grep 1 , .Xr lex 1 , .Xr printf 1 , .Xr sed 1 , .Xr strftime 3 , .Xr re_format 7 , .Xr script 7 .Rs .\" 4.4BSD USD:16 .\".%R Computing Science Technical Report .\".%N 68 .\".%D July 1978 .%A A. V. Aho .%A P. J. Weinberger .%A B. W. Kernighan .%T AWK \(em A Pattern Scanning and Processing Language .%J Software \(em Practice and Experience .%V 9:4 .%P pp. 267-279 .%D April 1979 .Re .Rs .%A A. V. Aho .%A B. W. Kernighan .%A P. J. Weinberger .%T The AWK Programming Language .%I Addison-Wesley .%D 2024 .%O ISBN 0-13-826972-6 .Re .Sh STANDARDS The .Nm utility is compliant with the .St -p1003.1-2024 specification except that consecutive backslashes in the replacement string argument for .Fn sub and .Fn gsub are not collapsed and a slash .Pq Ql / does not need to be escaped in a bracket expression. Also, the behaviour of .Fn rand and .Fn srand has been changed to support non-deterministic random numbers. .Pp The flags .Op Fl \&dV , .Op Fl -csv , and .Op Fl safe , support for regular expressions in .Va RS , as well as the functions .Fn fflush , .Fn gensub , .Fn compl , .Fn and , .Fn or , .Fn xor , .Fn lshift , .Fn rshift , .Fn mktime , .Fn strftime and .Fn systime are extensions to that specification. .Sh HISTORY An .Nm utility appeared in .At v7 . .Sh BUGS There are no explicit conversions between numbers and strings. To force an expression to be treated as a number add 0 to it; to force it to be treated as a string concatenate .Li \&"" to it. .Pp The scope rules for variables in functions are a botch; the syntax is worse.