1*0a6a1f1dSLionel Sambuc.\" $NetBSD: awk.1,v 1.4 2015/04/06 14:36:41 wiz Exp $ 25ea9e707SThomas Veerman.\" 35ea9e707SThomas Veerman.\" Copyright (C) Lucent Technologies 1997 45ea9e707SThomas Veerman.\" All Rights Reserved 55ea9e707SThomas Veerman.\" 65ea9e707SThomas Veerman.\" Permission to use, copy, modify, and distribute this software and 75ea9e707SThomas Veerman.\" its documentation for any purpose and without fee is hereby 85ea9e707SThomas Veerman.\" granted, provided that the above copyright notice appear in all 95ea9e707SThomas Veerman.\" copies and that both that the copyright notice and this 105ea9e707SThomas Veerman.\" permission notice and warranty disclaimer appear in supporting 115ea9e707SThomas Veerman.\" documentation, and that the name Lucent Technologies or any of 125ea9e707SThomas Veerman.\" its entities not be used in advertising or publicity pertaining 135ea9e707SThomas Veerman.\" to distribution of the software without specific, written prior 145ea9e707SThomas Veerman.\" permission. 155ea9e707SThomas Veerman.\" 165ea9e707SThomas Veerman.\" LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 175ea9e707SThomas Veerman.\" INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 185ea9e707SThomas Veerman.\" IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 195ea9e707SThomas Veerman.\" SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 205ea9e707SThomas Veerman.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 215ea9e707SThomas Veerman.\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 225ea9e707SThomas Veerman.\" ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 235ea9e707SThomas Veerman.\" THIS SOFTWARE. 245ea9e707SThomas Veerman.\" 25*0a6a1f1dSLionel Sambuc.Dd April 6, 2015 265ea9e707SThomas Veerman.Dt AWK 1 275ea9e707SThomas Veerman.Os 285ea9e707SThomas Veerman.Sh NAME 295ea9e707SThomas Veerman.Nm awk 305ea9e707SThomas Veerman.Nd pattern-directed scanning and processing language 315ea9e707SThomas Veerman.Sh SYNOPSIS 325ea9e707SThomas Veerman.Nm 335ea9e707SThomas Veerman.Op Fl F Ar fs 345ea9e707SThomas Veerman.Op Fl v Ar var=value 355ea9e707SThomas Veerman.Op Fl safe 365ea9e707SThomas Veerman.Op Fl d Ns Op Ar N 375ea9e707SThomas Veerman.Op Ar prog | Fl f Ar filename 385ea9e707SThomas Veerman.Ar 395ea9e707SThomas Veerman.Nm 405ea9e707SThomas Veerman.Fl version 415ea9e707SThomas Veerman.Sh DESCRIPTION 425ea9e707SThomas Veerman.Nm 435ea9e707SThomas Veermanis the Bell Labs' implementation of the AWK programming language as 445ea9e707SThomas Veermandescribed in the 455ea9e707SThomas Veerman.Em The AWK Programming Language 465ea9e707SThomas Veermanby 475ea9e707SThomas VeermanA. V. Aho, B. W. Kernighan, and P. J. Weinberger. 485ea9e707SThomas Veerman.Pp 495ea9e707SThomas Veerman.Nm 505ea9e707SThomas Veermanscans each input 515ea9e707SThomas Veerman.Ar file 525ea9e707SThomas Veermanfor lines that match any of a set of patterns specified literally in 535ea9e707SThomas Veerman.Ar prog 545ea9e707SThomas Veermanor in one or more files 555ea9e707SThomas Veermanspecified as 565ea9e707SThomas Veerman.Fl f Ar filename . 575ea9e707SThomas VeermanWith each pattern 585ea9e707SThomas Veermanthere can be an associated action that will be performed 595ea9e707SThomas Veermanwhen a line of a 605ea9e707SThomas Veerman.Ar file 615ea9e707SThomas Veermanmatches the pattern. 625ea9e707SThomas VeermanEach line is matched against the 635ea9e707SThomas Veermanpattern portion of every pattern-action statement; 645ea9e707SThomas Veermanthe associated action is performed for each matched pattern. 655ea9e707SThomas VeermanThe file name 665ea9e707SThomas Veerman.Ar - 675ea9e707SThomas Veermanmeans the standard input. 685ea9e707SThomas VeermanAny 695ea9e707SThomas Veerman.Ar file 705ea9e707SThomas Veermanof the form 715ea9e707SThomas Veerman.Ar var=value 725ea9e707SThomas Veermanis treated as an assignment, not a filename, 735ea9e707SThomas Veermanand is executed at the time it would have been opened if it were a filename. 745ea9e707SThomas Veerman.Pp 755ea9e707SThomas VeermanThe options are as follows: 765ea9e707SThomas Veerman.Bl -tag -width indent 775ea9e707SThomas Veerman.It Fl d Ns Op Ar N 785ea9e707SThomas VeermanSet debug level to specified number 795ea9e707SThomas Veerman.Ar N . 805ea9e707SThomas VeermanIf the number is omitted, debug level is set to 1. 815ea9e707SThomas Veerman.It Fl f Ar filename 825ea9e707SThomas VeermanRead the AWK program source from specified file 835ea9e707SThomas Veerman.Ar filename , 845ea9e707SThomas Veermaninstead of the first command line argument. 855ea9e707SThomas VeermanMultiple 865ea9e707SThomas Veerman.Fl f 875ea9e707SThomas Veermanoptions may be specified. 885ea9e707SThomas Veerman.It Fl F Ar fs 895ea9e707SThomas VeermanSet the input field separator 905ea9e707SThomas Veerman.Va FS 915ea9e707SThomas Veermanto the regular expression 925ea9e707SThomas Veerman.Ar fs . 935ea9e707SThomas Veerman.It Fl mr Ar NNN , Fl mf Ar NNN 945ea9e707SThomas VeermanObsolete, no longer needed options. 955ea9e707SThomas VeermanSet limit on maximum record or 965ea9e707SThomas Veermanfields number. 975ea9e707SThomas Veerman.It Fl safe 985ea9e707SThomas VeermanPotentially unsafe functions such as 995ea9e707SThomas Veerman.Fn system 1005ea9e707SThomas Veermanmake the program abort (with a warning message). 1015ea9e707SThomas Veerman.It Fl v Ar var Ns = Ns Ar value 1025ea9e707SThomas VeermanAssign the value 1035ea9e707SThomas Veerman.Ar value 1045ea9e707SThomas Veermanto the variable 1055ea9e707SThomas Veerman.Va var 1065ea9e707SThomas Veermanbefore 1075ea9e707SThomas Veerman.Ar prog 1085ea9e707SThomas Veermanis executed. 1095ea9e707SThomas VeermanAny number of 1105ea9e707SThomas Veerman.Fl v 1115ea9e707SThomas Veermanoptions may be present. 1125ea9e707SThomas Veerman.It Fl version 1135ea9e707SThomas VeermanPrint 1145ea9e707SThomas Veerman.Nm 1155ea9e707SThomas Veermanversion on standard output and exit. 1165ea9e707SThomas Veerman.El 1175ea9e707SThomas Veerman.Pp 1185ea9e707SThomas VeermanAn input line is normally made up of fields separated by white space, 1195ea9e707SThomas Veermanor by regular expression 1205ea9e707SThomas Veerman.Va FS . 1215ea9e707SThomas VeermanThe fields are denoted 1225ea9e707SThomas Veerman.Va $1 , 1235ea9e707SThomas Veerman.Va $2 , 1245ea9e707SThomas Veerman\&..., while 1255ea9e707SThomas Veerman.Va $0 1265ea9e707SThomas Veermanrefers to the entire line. 1275ea9e707SThomas VeermanIf 1285ea9e707SThomas Veerman.Va FS 1295ea9e707SThomas Veermanis null, the input line is split into one field per character. 1305ea9e707SThomas Veerman.Pp 1315ea9e707SThomas VeermanA pattern-action statement has the form 1325ea9e707SThomas Veerman.Lp 1335ea9e707SThomas Veerman.Dl pattern \&{ action \&} 1345ea9e707SThomas Veerman.Lp 1355ea9e707SThomas VeermanA missing \&{ action \&} 1365ea9e707SThomas Veermanmeans print the line; 1375ea9e707SThomas Veermana missing pattern always matches. 1385ea9e707SThomas VeermanPattern-action statements are separated by newlines or semicolons. 1395ea9e707SThomas Veerman.Pp 1405ea9e707SThomas VeermanAn action is a sequence of statements. 1415ea9e707SThomas VeermanStatements are terminated by 1425ea9e707SThomas Veermansemicolons, newlines or right braces. 1435ea9e707SThomas VeermanAn empty 1445ea9e707SThomas Veerman.Ar expression-list 1455ea9e707SThomas Veermanstands for 1465ea9e707SThomas Veerman.Va $0 . 1475ea9e707SThomas VeermanString constants are quoted 1485ea9e707SThomas Veerman.Em \&"\ \&" , 1495ea9e707SThomas Veermanwith the usual C escapes recognized within. 1505ea9e707SThomas VeermanExpressions take on string or numeric values as appropriate, 1515ea9e707SThomas Veermanand are built using the 1525ea9e707SThomas Veerman.Sx Operators 1535ea9e707SThomas Veerman(see next subsection). 1545ea9e707SThomas VeermanVariables may be scalars, array elements 1555ea9e707SThomas Veerman(denoted 1565ea9e707SThomas Veerman.Va x[i] ) 1575ea9e707SThomas Veermanor fields. 1585ea9e707SThomas VeermanVariables are initialized to the null string. 1595ea9e707SThomas VeermanArray subscripts may be any string, 1605ea9e707SThomas Veermannot necessarily numeric; 1615ea9e707SThomas Veermanthis allows for a form of associative memory. 1625ea9e707SThomas VeermanMultiple subscripts such as 1635ea9e707SThomas Veerman.Va [i,j,k] 1645ea9e707SThomas Veermanare permitted; the constituents are concatenated, 1655ea9e707SThomas Veermanseparated by the value of 1665ea9e707SThomas Veerman.Va SUBSEP . 1675ea9e707SThomas Veerman.Ss Operators 1685ea9e707SThomas Veerman.Nm 1695ea9e707SThomas Veermanoperators, in order of decreasing precedence, are: 1705ea9e707SThomas Veerman.Pp 1715ea9e707SThomas Veerman.Bl -tag -width ident -compact 1725ea9e707SThomas Veerman.It Ic (...) 1735ea9e707SThomas VeermanGrouping 1745ea9e707SThomas Veerman.It Ic $ 1755ea9e707SThomas VeermanField reference 1765ea9e707SThomas Veerman.It Ic ++ -- 1775ea9e707SThomas VeermanIncrement and decrement, can be used either as postfix or prefix. 1785ea9e707SThomas Veerman.It Ic ^ 1795ea9e707SThomas VeermanExponentiation (the 1805ea9e707SThomas Veerman.Ic ** 1815ea9e707SThomas Veermanform is also supported, and 1825ea9e707SThomas Veerman.Ic **= 1835ea9e707SThomas Veermanfor the assignment operator). 1845ea9e707SThomas Veerman.It + - \&! 1855ea9e707SThomas VeermanUnary plus, unary minus and logical negation. 1865ea9e707SThomas Veerman.It * / % 1875ea9e707SThomas VeermanMultiplication, division and modulus. 1885ea9e707SThomas Veerman.It + - 1895ea9e707SThomas VeermanAddition and subtraction. 1905ea9e707SThomas Veerman.It Ar space 1915ea9e707SThomas VeermanString concatenation. 1925ea9e707SThomas Veerman.It Ic \*[Lt] \*[Gt] 1935ea9e707SThomas Veerman.It Ic \*[Le] \*[Ge] 1945ea9e707SThomas Veerman.It Ic != == 1955ea9e707SThomas VeermanRegular relational operators 1965ea9e707SThomas Veerman.It Ic ~ !~ 1975ea9e707SThomas VeermanRegular expression match and not match 1985ea9e707SThomas Veerman.It Ic in 1995ea9e707SThomas VeermanArray membership 2005ea9e707SThomas Veerman.It Ic "\*[Am]\*[Am]" 2015ea9e707SThomas VeermanLogical AND 2025ea9e707SThomas Veerman.It Ic "||" 2035ea9e707SThomas VeermanLogical OR 2045ea9e707SThomas Veerman.It Ic ?: 2055ea9e707SThomas VeermanC conditional expression. 2065ea9e707SThomas VeermanThis is used as 2075ea9e707SThomas Veerman.Ar expr1 Ic \&? Ar expr2 Ic \&: Ar expr3 No . 2085ea9e707SThomas VeermanIf 2095ea9e707SThomas Veerman.Ar expr1 2105ea9e707SThomas Veermanis true, the result value is 2115ea9e707SThomas Veerman.Ar expr2 , 2125ea9e707SThomas Veermanotherwise it is 2135ea9e707SThomas Veerman.Ar expr3 . 2145ea9e707SThomas VeermanOnly one of 2155ea9e707SThomas Veerman.Ar expr2 2165ea9e707SThomas Veermanand 2175ea9e707SThomas Veerman.Ar expr3 2185ea9e707SThomas Veermanis evaluated. 2195ea9e707SThomas Veerman.It Ic = += -= 2205ea9e707SThomas Veerman.It Ic *= /= %= ^= 2215ea9e707SThomas VeermanAssignment and Operator-Assignment 2225ea9e707SThomas Veerman.El 2235ea9e707SThomas Veerman.Ss Control Statements 2245ea9e707SThomas VeermanThe control statements are as follows: 2255ea9e707SThomas Veerman.Pp 2265ea9e707SThomas Veerman.Bl -hang -offset indent -width indent -compact 2275ea9e707SThomas Veerman.It Ic if \&( Ar expression Ic \&) Ar statement Bq Ic else Ar statement 2285ea9e707SThomas Veerman.It Ic while \&( Ar expression Ic \&) Ar statement 2295ea9e707SThomas Veerman.It Ic for \&( Ar expression Ic \&; Ar expression Ic \&; \ 2305ea9e707SThomas VeermanAr expression Ic \&) Ar statement 2315ea9e707SThomas Veerman.It Ic for \&( Va var Ic in Ar array Ic \&) Ar statement 2325ea9e707SThomas Veerman.It Ic do Ar statement Ic while \&( Ar expression Ic \&) 2335ea9e707SThomas Veerman.It Ic break 2345ea9e707SThomas Veerman.It Ic continue 2355ea9e707SThomas Veerman.It Ic delete Va array Bq Ar expression 2365ea9e707SThomas Veerman.It Ic delete Va array 2375ea9e707SThomas Veerman.It Ic exit Bq Ar expression 2385ea9e707SThomas Veerman.Ar expression 2395ea9e707SThomas Veerman.It Ic return Bq Ar expression 2405ea9e707SThomas Veerman.It Ic \&{ Ar [ statement ... ] Ic \&} 2415ea9e707SThomas Veerman.El 2425ea9e707SThomas Veerman.Ss I/O Statements 2435ea9e707SThomas VeermanThe input/output statements are as follows: 2445ea9e707SThomas Veerman.Pp 2455ea9e707SThomas Veerman.Bl -tag -width indent 2465ea9e707SThomas Veerman.It Fn close expr 2475ea9e707SThomas VeermanCloses the file or pipe 2485ea9e707SThomas Veerman.Ar expr . 2495ea9e707SThomas VeermanReturns zero on success; otherwise nonzero. 2505ea9e707SThomas Veerman.It Fn fflush expr 2515ea9e707SThomas VeermanFlushes any buffered output for the file or pipe 2525ea9e707SThomas Veerman.Ar expr . 2535ea9e707SThomas VeermanReturns zero on success; otherwise nonzero. 2545ea9e707SThomas Veerman.It Ic getline Bq Va var 2555ea9e707SThomas VeermanSet 2565ea9e707SThomas Veerman.Va var 2575ea9e707SThomas Veerman(or 2585ea9e707SThomas Veerman.Va $0 if 2595ea9e707SThomas Veerman.Va var 2605ea9e707SThomas Veermanis not specified) 2615ea9e707SThomas Veermanto the next input record from the current input file. 2625ea9e707SThomas Veerman.Ic getline 2635ea9e707SThomas Veermanreturns 1 for a successful input, 2645ea9e707SThomas Veerman0 for end of file, and \-1 for an error. 2655ea9e707SThomas Veerman.It Ic getline Bo Va var Bc Ic \*[Lt] Ar file 2665ea9e707SThomas VeermanSet 2675ea9e707SThomas Veerman.Va var 2685ea9e707SThomas Veerman(or 2695ea9e707SThomas Veerman.Va $0 if 2705ea9e707SThomas Veerman.Va var 2715ea9e707SThomas Veermanis not specified) 2725ea9e707SThomas Veermanto the next input record from the specified file 2735ea9e707SThomas Veerman.Ar file . 2745ea9e707SThomas Veerman.It Ar expr Ic \&| getline 2755ea9e707SThomas VeermanPipes the output of 2765ea9e707SThomas Veerman.Ar expr 2775ea9e707SThomas Veermaninto 2785ea9e707SThomas Veerman.Ic getline ; 2795ea9e707SThomas Veermaneach call of 2805ea9e707SThomas Veerman.Ic getline 2815ea9e707SThomas Veermanreturns the next line of output from 2825ea9e707SThomas Veerman.Ar expr . 2835ea9e707SThomas Veerman.It Ic next 2845ea9e707SThomas VeermanSkip remaining patterns on this input line. 2855ea9e707SThomas Veerman.It Ic nextfile 2865ea9e707SThomas VeermanSkip rest of this file, open next, start at top. 2875ea9e707SThomas Veerman.It Ic print Bo Ar expr-list Bc Bq Ic \*[Gt] Ar file 2885ea9e707SThomas VeermanThe 2895ea9e707SThomas Veerman.Ic print 2905ea9e707SThomas Veermanstatement prints its arguments on the standard output (or to a file 2915ea9e707SThomas Veermanif 2925ea9e707SThomas Veerman.Ic \*[Gt] file 2935ea9e707SThomas Veermanor to a pipe if 2945ea9e707SThomas Veerman.Ic | Ar expr 2955ea9e707SThomas Veermanis present), 2965ea9e707SThomas Veermanseparated by the current output field separator 2975ea9e707SThomas Veerman.Va OFS , 2985ea9e707SThomas Veermanand terminated by the 2995ea9e707SThomas Veermanoutput record separator 3005ea9e707SThomas Veerman.Va ORS . 3015ea9e707SThomas VeermanBoth 3025ea9e707SThomas Veerman.Ar file 3035ea9e707SThomas Veermanand 3045ea9e707SThomas Veerman.Ar expr 3055ea9e707SThomas Veermanmay be literal names or parenthesized expressions; identical string values in 3065ea9e707SThomas Veermandifferent statements denote the same open file. 3075ea9e707SThomas Veerman.It Ic printf Ar format Bo Ic \&, Ar expr-list Bc Bq Ic \*[Gt] Ar file 3085ea9e707SThomas VeermanFormat and print its expression list according to 3095ea9e707SThomas Veerman.Ar format . 3105ea9e707SThomas VeermanSee 3115ea9e707SThomas Veerman.Xr printf 3 3125ea9e707SThomas Veermanfor list of supported formats and their meaning. 3135ea9e707SThomas Veerman.El 3145ea9e707SThomas Veerman.Ss Mathematical and Numeric Functions 3155ea9e707SThomas VeermanAWK has the following mathematical and numerical functions built-in: 3165ea9e707SThomas Veerman.Pp 3175ea9e707SThomas Veerman.Bl -tag -width indent 3185ea9e707SThomas Veerman.It Fn atan2 x y 3195ea9e707SThomas VeermanReturns the arctangent of 3205ea9e707SThomas Veerman.Ar x Ic / Ar y 3215ea9e707SThomas Veermanin radians. 3225ea9e707SThomas VeermanSee also 3235ea9e707SThomas Veerman.Xr atan2 3 . 3245ea9e707SThomas Veerman.It Fn cos expr 3255ea9e707SThomas VeermanComputes the cosine of 3265ea9e707SThomas Veerman.Ar expr , 3275ea9e707SThomas Veermanmeasured in radians. 3285ea9e707SThomas VeermanSee also 3295ea9e707SThomas Veerman.Xr cos 3 . 3305ea9e707SThomas Veerman.It Fn exp expr 3315ea9e707SThomas VeermanComputes the exponential value of the given argument 3325ea9e707SThomas Veerman.Ar expr . 3335ea9e707SThomas VeermanSee also 3345ea9e707SThomas Veerman.Xr exp 3 . 3355ea9e707SThomas Veerman.It Fn int expr 3365ea9e707SThomas VeermanTruncates 3375ea9e707SThomas Veerman.Ar expr 3385ea9e707SThomas Veermanto integer. 3395ea9e707SThomas Veerman.It Fn log expr 3405ea9e707SThomas VeermanComputes the value of the natural logarithm of argument 3415ea9e707SThomas Veerman.Ar expr . 3425ea9e707SThomas VeermanSee also 3435ea9e707SThomas Veerman.Xr log 3 . 3445ea9e707SThomas Veerman.It Fn rand 3455ea9e707SThomas VeermanReturns random number between 0 and 1. 3465ea9e707SThomas Veerman.It Fn sin expr 3475ea9e707SThomas VeermanComputes the sine of 3485ea9e707SThomas Veerman.Ar expr , 3495ea9e707SThomas Veermanmeasured in radians. 3505ea9e707SThomas VeermanSee also 3515ea9e707SThomas Veerman.Xr sin 3 . 3525ea9e707SThomas Veerman.It Fn sqrt expr 3535ea9e707SThomas VeermanComputes the non-negative square root of 3545ea9e707SThomas Veerman.Ar expr . 3555ea9e707SThomas VeermanSee also 3565ea9e707SThomas Veerman.Xr sqrt 3 . 3575ea9e707SThomas Veerman.It Fn srand [expr] 3585ea9e707SThomas VeermanSets seed for random number generator ( 3595ea9e707SThomas Veerman.Fn rand ) 3605ea9e707SThomas Veermanand returns the previous seed. 3615ea9e707SThomas Veerman.El 3625ea9e707SThomas Veerman.Ss String Functions 3635ea9e707SThomas VeermanAWK has the following string functions built-in: 3645ea9e707SThomas Veerman.Pp 3655ea9e707SThomas Veerman.Bl -tag -width indent 3665ea9e707SThomas Veerman.It Fn gensub r s h [t] 3675ea9e707SThomas VeermanSearch the target string 3685ea9e707SThomas Veerman.Ar t 3695ea9e707SThomas Veermanfor matches of the regular expression 3705ea9e707SThomas Veerman.Ar r . 3715ea9e707SThomas VeermanIf 3725ea9e707SThomas Veerman.Ar h 3735ea9e707SThomas Veermanis a string beginning with 3745ea9e707SThomas Veerman.Ic g 3755ea9e707SThomas Veermanor 3765ea9e707SThomas Veerman.Ic G , 3775ea9e707SThomas Veermanthen replace all matches of 3785ea9e707SThomas Veerman.Ar r 3795ea9e707SThomas Veermanwith 3805ea9e707SThomas Veerman.Ar s . 3815ea9e707SThomas VeermanOtherwise, 3825ea9e707SThomas Veerman.Ar h 3835ea9e707SThomas Veermanis a number indicating which match of 3845ea9e707SThomas Veerman.Ar r 3855ea9e707SThomas Veermanto replace. 3865ea9e707SThomas VeermanIf no 3875ea9e707SThomas Veerman.Ar t 3885ea9e707SThomas Veermanis supplied, 3895ea9e707SThomas Veerman.Va $0 3905ea9e707SThomas Veermanis used instead. 3915ea9e707SThomas Veerman.\"Within the replacement text 3925ea9e707SThomas Veerman.\".Ar s , 3935ea9e707SThomas Veerman.\"the sequence 3945ea9e707SThomas Veerman.\".Ar \en , 3955ea9e707SThomas Veerman.\"where 3965ea9e707SThomas Veerman.\".Ar n 3975ea9e707SThomas Veerman.\"is a digit from 1 to 9, may be used to indicate just the text that 3985ea9e707SThomas Veerman.\"matched the 3995ea9e707SThomas Veerman.\".Ar n Ap th 4005ea9e707SThomas Veerman.\"parenthesized subexpression. 4015ea9e707SThomas Veerman.\"The sequence 4025ea9e707SThomas Veerman.\".Ic \e0 4035ea9e707SThomas Veerman.\"represents the entire text, as does the character 4045ea9e707SThomas Veerman.\".Ic & . 4055ea9e707SThomas VeermanUnlike 4065ea9e707SThomas Veerman.Fn sub 4075ea9e707SThomas Veermanand 4085ea9e707SThomas Veerman.Fn gsub , 4095ea9e707SThomas Veermanthe modified string is returned as the result of the function, 4105ea9e707SThomas Veermanand the original target is 4115ea9e707SThomas Veerman.Em not 4125ea9e707SThomas Veermanchanged. 4135ea9e707SThomas VeermanNote that the 4145ea9e707SThomas Veerman.Ar \en 4155ea9e707SThomas Veermansequences within replacement string 4165ea9e707SThomas Veerman.Ar s 4175ea9e707SThomas Veermansupported by GNU 4185ea9e707SThomas Veerman.Nm 4195ea9e707SThomas Veermanare 4205ea9e707SThomas Veerman.Em not 4215ea9e707SThomas Veermansupported at this moment. 422*0a6a1f1dSLionel Sambuc.It Fn gsub r s "[t]" 423*0a6a1f1dSLionel SambucSame as 4245ea9e707SThomas Veerman.Fn sub 4255ea9e707SThomas Veermanexcept that all occurrences of the regular expression 4265ea9e707SThomas Veermanare replaced; 4275ea9e707SThomas Veerman.Fn sub 4285ea9e707SThomas Veermanand 4295ea9e707SThomas Veerman.Fn gsub 4305ea9e707SThomas Veermanreturn the number of replacements. 4315ea9e707SThomas Veerman.It Fn index s t 4325ea9e707SThomas Veermanthe position in 4335ea9e707SThomas Veerman.Ar s 4345ea9e707SThomas Veermanwhere the string 4355ea9e707SThomas Veerman.Ar t 4365ea9e707SThomas Veermanoccurs, or 0 if it does not. 4375ea9e707SThomas Veerman.It Fn length "[string]" 4385ea9e707SThomas Veermanthe length of its argument 4395ea9e707SThomas Veermantaken as a string, 4405ea9e707SThomas Veermanor of 4415ea9e707SThomas Veerman.Va $0 4425ea9e707SThomas Veermanif no argument. 4435ea9e707SThomas Veerman.It Fn match s r 4445ea9e707SThomas Veermanthe position in 4455ea9e707SThomas Veerman.Ar s 4465ea9e707SThomas Veermanwhere the regular expression 4475ea9e707SThomas Veerman.Ar r 4485ea9e707SThomas Veermanoccurs, or 0 if it does not. 4495ea9e707SThomas VeermanThe variables 4505ea9e707SThomas Veerman.Va RSTART 4515ea9e707SThomas Veermanand 4525ea9e707SThomas Veerman.Va RLENGTH 4535ea9e707SThomas Veermanare set to the position and length of the matched string. 4545ea9e707SThomas Veerman.It Fn split s a "[fs]" 4555ea9e707SThomas Veermansplits the string 4565ea9e707SThomas Veerman.Ar s 4575ea9e707SThomas Veermaninto array elements 4585ea9e707SThomas Veerman.Va a[1] , 4595ea9e707SThomas Veerman.Va a[2] , 4605ea9e707SThomas Veerman\&..., 4615ea9e707SThomas Veerman.Va a[n] , 4625ea9e707SThomas Veermanand returns 4635ea9e707SThomas Veerman.Va n . 4645ea9e707SThomas VeermanThe separation is done with the regular expression 4655ea9e707SThomas Veerman.Ar fs 4665ea9e707SThomas Veermanor with the field separator 4675ea9e707SThomas Veerman.Va FS 4685ea9e707SThomas Veermanif 4695ea9e707SThomas Veerman.Ar fs 4705ea9e707SThomas Veermanis not given. 4715ea9e707SThomas VeermanAn empty string as field separator splits the string 4725ea9e707SThomas Veermaninto one array element per character. 4735ea9e707SThomas Veerman.It Fn sprintf fmt expr "..." 4745ea9e707SThomas VeermanReturns the string resulting from formatting 4755ea9e707SThomas Veerman.Ar expr 4765ea9e707SThomas Veermanaccording to the 4775ea9e707SThomas Veerman.Xr printf 3 4785ea9e707SThomas Veermanformat 4795ea9e707SThomas Veerman.Ar fmt . 480*0a6a1f1dSLionel Sambuc.It Fn sub r s "[t]" 4815ea9e707SThomas Veermansubstitutes 482*0a6a1f1dSLionel Sambuc.Ar s 4835ea9e707SThomas Veermanfor the first occurrence of the regular expression 4845ea9e707SThomas Veerman.Ar r 485*0a6a1f1dSLionel Sambucin the target string 486*0a6a1f1dSLionel Sambuc.Ar t . 4875ea9e707SThomas VeermanIf 488*0a6a1f1dSLionel Sambuc.Ar t 4895ea9e707SThomas Veermanis not given, 4905ea9e707SThomas Veerman.Va $0 4915ea9e707SThomas Veermanis used. 4925ea9e707SThomas Veerman.It Fn substr s m [n] 4935ea9e707SThomas VeermanReturns the at most 4945ea9e707SThomas Veerman.Ar n Ns No -character 4955ea9e707SThomas Veermansubstring of 4965ea9e707SThomas Veerman.Ar s 4975ea9e707SThomas Veermanstarting at position 4985ea9e707SThomas Veerman.Ar m , 4995ea9e707SThomas Veermancounted from 1. 5005ea9e707SThomas VeermanIf 5015ea9e707SThomas Veerman.Ar n 5025ea9e707SThomas Veermanis omitted, the rest of 5035ea9e707SThomas Veerman.Ar s 5045ea9e707SThomas Veermanis returned. 5055ea9e707SThomas Veerman.It Fn tolower str 5065ea9e707SThomas Veermanreturns a copy of 5075ea9e707SThomas Veerman.Ar str 5085ea9e707SThomas Veermanwith all upper-case characters translated to their 5095ea9e707SThomas Veermancorresponding lower-case equivalents. 5105ea9e707SThomas Veerman.It Fn toupper str 5115ea9e707SThomas Veermanreturns a copy of 5125ea9e707SThomas Veerman.Ar str 5135ea9e707SThomas Veermanwith all lower-case characters translated to their 5145ea9e707SThomas Veermancorresponding upper-case equivalents. 5155ea9e707SThomas Veerman.El 5165ea9e707SThomas Veerman.Ss Time Functions 5175ea9e707SThomas VeermanThis 5185ea9e707SThomas Veerman.Nm 5195ea9e707SThomas Veermanprovides the following two functions for obtaining time 5205ea9e707SThomas Veermanstamps and formatting them: 5215ea9e707SThomas Veerman.Bl -tag -width indent 5225ea9e707SThomas Veerman.It Fn systime 5235ea9e707SThomas VeermanReturns the value of time in seconds since the start of 5245ea9e707SThomas Veerman.Tn Unix 5255ea9e707SThomas VeermanEpoch (Midnight, January 1, 1970, Coordinated Universal Time). 5265ea9e707SThomas VeermanSee also 5275ea9e707SThomas Veerman.Xr time 3 . 5285ea9e707SThomas Veerman.It Fn strftime "[format [, timestamp]]" 5295ea9e707SThomas VeermanFormats the time 5305ea9e707SThomas Veerman.Ar timestamp 5315ea9e707SThomas Veermanaccording to the string 5325ea9e707SThomas Veerman.Ar format . 5335ea9e707SThomas Veerman.Ar timestamp 5345ea9e707SThomas Veermanshould be in same form as value returned by 5355ea9e707SThomas Veerman.Fn systime . 5365ea9e707SThomas VeermanIf 5375ea9e707SThomas Veerman.Ar timestamp 5385ea9e707SThomas Veermanis missing, current time is used. 5395ea9e707SThomas VeermanIf 5405ea9e707SThomas Veerman.Ar format 5415ea9e707SThomas Veermanis missing, a default format equivalent to the output of 5425ea9e707SThomas Veerman.Xr date 1 5435ea9e707SThomas Veermanwould be used. 5445ea9e707SThomas VeermanSee the specification of ANSI C 5455ea9e707SThomas Veerman.Xr strftime 3 5465ea9e707SThomas Veermanfor the format conversions which are supported. 5475ea9e707SThomas Veerman.El 5485ea9e707SThomas Veerman.Ss Other built-in functions 5495ea9e707SThomas Veerman.Bl -tag -width indent 5505ea9e707SThomas Veerman.It Fn system cmd 5515ea9e707SThomas Veermanexecutes 5525ea9e707SThomas Veerman.Ar cmd 5535ea9e707SThomas Veermanand returns its exit status 5545ea9e707SThomas Veerman.El 5555ea9e707SThomas Veerman.Ss Patterns 5565ea9e707SThomas VeermanPatterns are arbitrary Boolean combinations 5575ea9e707SThomas Veerman(with 5585ea9e707SThomas Veerman.Ic "! || \*[Am]\*[Am]" ) 5595ea9e707SThomas Veermanof regular expressions and 5605ea9e707SThomas Veermanrelational expressions. 5615ea9e707SThomas VeermanRegular expressions are as in 5625ea9e707SThomas Veerman.Xr egrep 1 . 5635ea9e707SThomas VeermanIsolated regular expressions 5645ea9e707SThomas Veermanin a pattern apply to the entire line. 5655ea9e707SThomas VeermanRegular expressions may also occur in 5665ea9e707SThomas Veermanrelational expressions, using the operators 5675ea9e707SThomas Veerman.Ic ~ 5685ea9e707SThomas Veermanand 5695ea9e707SThomas Veerman.Ic !~ . 5705ea9e707SThomas Veerman.Ic / re / 5715ea9e707SThomas Veermanis a constant regular expression; 5725ea9e707SThomas Veermanany string (constant or variable) may be used 5735ea9e707SThomas Veermanas a regular expression, except in the position of an isolated regular expression 5745ea9e707SThomas Veermanin a pattern. 5755ea9e707SThomas Veerman.Pp 5765ea9e707SThomas VeermanA pattern may consist of two patterns separated by a comma; 5775ea9e707SThomas Veermanin this case, the action is performed for all lines 5785ea9e707SThomas Veermanfrom an occurrence of the first pattern 5795ea9e707SThomas Veermanthough an occurrence of the second. 5805ea9e707SThomas Veerman.Pp 5815ea9e707SThomas VeermanA relational expression is one of the following: 5825ea9e707SThomas Veerman.Bl -tag -offset indent -width indent -compact 5835ea9e707SThomas Veerman.It Ar expression matchop regular-expression 5845ea9e707SThomas Veerman.It Ar expression relop expression 5855ea9e707SThomas Veerman.It Ar expression Ic in Ar array-name 5865ea9e707SThomas Veerman.It ( Ar expr , expr,\&... Ic ") in" Ar array-name 5875ea9e707SThomas Veerman.El 5885ea9e707SThomas Veerman.Pp 5895ea9e707SThomas Veermanwhere a 5905ea9e707SThomas Veerman.Ar relop 5915ea9e707SThomas Veermanis any of the six relational operators in C, 5925ea9e707SThomas Veermanand a 5935ea9e707SThomas Veerman.Ar matchop 5945ea9e707SThomas Veermanis either 5955ea9e707SThomas Veerman.Ic ~ 5965ea9e707SThomas Veerman(matches) 5975ea9e707SThomas Veermanor 5985ea9e707SThomas Veerman.Ic !~ 5995ea9e707SThomas Veerman(does not match). 6005ea9e707SThomas VeermanA conditional is an arithmetic expression, 6015ea9e707SThomas Veermana relational expression, 6025ea9e707SThomas Veermanor a Boolean combination 6035ea9e707SThomas Veermanof these. 6045ea9e707SThomas Veerman.Pp 6055ea9e707SThomas VeermanThe special patterns 6065ea9e707SThomas Veerman.Ic BEGIN 6075ea9e707SThomas Veermanand 6085ea9e707SThomas Veerman.Ic END 6095ea9e707SThomas Veermanmay be used to capture control before the first input line is read 6105ea9e707SThomas Veermanand after the last. 6115ea9e707SThomas Veerman.Ic BEGIN 6125ea9e707SThomas Veermanand 6135ea9e707SThomas Veerman.Ic END 6145ea9e707SThomas Veermando not combine with other patterns. 6155ea9e707SThomas Veerman.Ss Built-in Variables 6165ea9e707SThomas VeermanVariable names with special meanings: 6175ea9e707SThomas Veerman.Bl -hang -width FILENAMES 6185ea9e707SThomas Veerman.It Va ARGC 6195ea9e707SThomas Veermanargument count, assignable 6205ea9e707SThomas Veerman.It Va ARGV 6215ea9e707SThomas Veermanargument array, assignable; 6225ea9e707SThomas Veermannon-null members are taken as filenames 6235ea9e707SThomas Veerman.It Va CONVFMT 6245ea9e707SThomas Veermanconversion format used when converting numbers 6255ea9e707SThomas Veerman(default 6265ea9e707SThomas Veerman.Qq %.6g ) 6275ea9e707SThomas Veerman.It Va ENVIRON 6285ea9e707SThomas Veermanarray of environment variables; subscripts are names. 6295ea9e707SThomas Veerman.It Va FILENAME 6305ea9e707SThomas Veermanthe name of the current input file 6315ea9e707SThomas Veerman.It Va FNR 6325ea9e707SThomas Veermanordinal number of the current record in the current file 6335ea9e707SThomas Veerman.It Va FS 6345ea9e707SThomas Veermanregular expression used to separate fields; also settable 6355ea9e707SThomas Veermanby option 6365ea9e707SThomas Veerman.Fl F Ar fs . 6375ea9e707SThomas Veerman.It Va NF 6385ea9e707SThomas Veermannumber of fields in the current record 6395ea9e707SThomas Veerman.It Va NR 6405ea9e707SThomas Veermanordinal number of the current record 6415ea9e707SThomas Veerman.It Va OFMT 6425ea9e707SThomas Veermanoutput format for numbers (default 6435ea9e707SThomas Veerman.Qq "%.6g" 6445ea9e707SThomas Veerman) 6455ea9e707SThomas Veerman.It Va OFS 6465ea9e707SThomas Veermanoutput field separator (default blank) 6475ea9e707SThomas Veerman.It Va ORS 6485ea9e707SThomas Veermanoutput record separator (default newline) 6495ea9e707SThomas Veerman.It Va RS 6505ea9e707SThomas Veermaninput record separator (default newline) 6515ea9e707SThomas Veerman.It Va RSTART 6525ea9e707SThomas VeermanPosition of the first character matched by 6535ea9e707SThomas Veerman.Fn match ; 6545ea9e707SThomas Veerman0 if not match. 6555ea9e707SThomas Veerman.It Va RLENGTH 6565ea9e707SThomas VeermanLength of the string matched by 6575ea9e707SThomas Veerman.Fn match ; 6585ea9e707SThomas Veerman-1 if no match. 6595ea9e707SThomas Veerman.It Va SUBSEP 6605ea9e707SThomas Veermanseparates multiple subscripts (default 034) 6615ea9e707SThomas Veerman.El 6625ea9e707SThomas Veerman.Ss Functions 6635ea9e707SThomas VeermanFunctions may be defined (at the position of a pattern-action statement) thus: 6645ea9e707SThomas Veerman.Bd -filled -offset indent 6655ea9e707SThomas Veerman.Ic function foo(a, b, c) { ...; return x } 6665ea9e707SThomas Veerman.Ed 6675ea9e707SThomas Veerman.Pp 6685ea9e707SThomas VeermanParameters are passed by value if scalar and by reference if array name; 6695ea9e707SThomas Veermanfunctions may be called recursively. 6705ea9e707SThomas VeermanParameters are local to the function; all other variables are global. 6715ea9e707SThomas VeermanThus local variables may be created by providing excess parameters in 6725ea9e707SThomas Veermanthe function definition. 6735ea9e707SThomas Veerman.Sh EXAMPLES 6745ea9e707SThomas Veerman.Bl -tag -width indent -compact 6755ea9e707SThomas Veerman.It Ic length($0) \*[Gt] 72 6765ea9e707SThomas VeermanPrint lines longer than 72 characters. 6775ea9e707SThomas Veerman.Pp 6785ea9e707SThomas Veerman.It Ic \&{ print $2, $1 \&} 6795ea9e707SThomas VeermanPrint first two fields in opposite order. 6805ea9e707SThomas Veerman.Pp 6815ea9e707SThomas Veerman.It Ic BEGIN { FS = \&",[ \et]*|[ \et]+\&" } 6825ea9e707SThomas Veerman.It Ic "\ \ \ \ \ \ {" print \&$2, \&$1 } 6835ea9e707SThomas VeermanSame, with input fields separated by comma and/or blanks and tabs. 6845ea9e707SThomas Veerman.Pp 6855ea9e707SThomas Veerman.It Ic "\ \ \ \ {" s += $1 } 6865ea9e707SThomas Veerman.It Ic END { print \&"sum is\&", s, \&" average is\ \&",\ s/NR\ } 6875ea9e707SThomas VeermanAdd up first column, print sum and average. 6885ea9e707SThomas Veerman.Pp 6895ea9e707SThomas Veerman.It Ic /start/, /stop/ 6905ea9e707SThomas VeermanPrint all lines between start/stop pairs. 6915ea9e707SThomas Veerman.Pp 6925ea9e707SThomas Veerman.It Ic BEGIN { # Simulate echo(1) 6935ea9e707SThomas Veerman.It Ic "\ \ \ \ " for (i = 1; i \*[Lt] ARGC;\ i++)\ printf\ \&"%s\ \&",\ ARGV[i] 6945ea9e707SThomas Veerman.It Ic "\ \ \ \ " printf \&"\en\&" 6955ea9e707SThomas Veerman.It Ic "\ \ \ \ " exit } 6965ea9e707SThomas Veerman.El 6975ea9e707SThomas Veerman.Sh SEE ALSO 6985ea9e707SThomas Veerman.Xr egrep 1 , 6995ea9e707SThomas Veerman.Xr lex 1 , 7005ea9e707SThomas Veerman.Xr sed 1 , 7015ea9e707SThomas Veerman.Xr atan2 3 , 7025ea9e707SThomas Veerman.Xr cos 3 , 7035ea9e707SThomas Veerman.Xr exp 3 , 7045ea9e707SThomas Veerman.Xr log 3 , 7055ea9e707SThomas Veerman.Xr sin 3 , 7065ea9e707SThomas Veerman.Xr sqrt 3 , 7075ea9e707SThomas Veerman.Xr strftime 3 , 7085ea9e707SThomas Veerman.Xr time 3 7095ea9e707SThomas Veerman.Pp 7105ea9e707SThomas VeermanA. V. Aho, B. W. Kernighan, P. J. Weinberger, 7115ea9e707SThomas Veerman.Em The AWK Programming Language , 7125ea9e707SThomas VeermanAddison-Wesley, 1988. 7135ea9e707SThomas VeermanISBN 0-201-07981-X 7145ea9e707SThomas Veerman.Pp 7155ea9e707SThomas Veerman.Em AWK Language Programming , 7165ea9e707SThomas VeermanEdition 1.0, published by the Free Software Foundation, 1995 7175ea9e707SThomas Veerman.Sh HISTORY 7185ea9e707SThomas Veerman.Nm nawk 7195ea9e707SThomas Veermanhas been the default system 7205ea9e707SThomas Veerman.Nm 7215ea9e707SThomas Veermansince 7225ea9e707SThomas Veerman.Nx 2.0 , 7235ea9e707SThomas Veermanreplacing the previously used GNU 7245ea9e707SThomas Veerman.Nm . 7255ea9e707SThomas Veerman.Sh BUGS 7265ea9e707SThomas VeermanThere are no explicit conversions between numbers and strings. 7275ea9e707SThomas VeermanTo force an expression to be treated as a number add 0 to it; 7285ea9e707SThomas Veermanto force it to be treated as a string concatenate 7295ea9e707SThomas Veerman\&"\&" to it. 7305ea9e707SThomas Veerman.Pp 7315ea9e707SThomas VeermanThe scope rules for variables in functions are a botch; 7325ea9e707SThomas Veermanthe syntax is worse. 733