awk.1 revision 272918
185587Sobrien.de EX 285587Sobrien.nf 385587Sobrien.ft CW 485587Sobrien.. 585587Sobrien.de EE 685587Sobrien.br 785587Sobrien.fi 885587Sobrien.ft 1 985587Sobrien.. 1085587Sobrienawk 1185587Sobrien.TH AWK 1 1285587Sobrien.CT 1 files prog_other 1385587Sobrien.SH NAME 1485587Sobrienawk \- pattern-directed scanning and processing language 1585587Sobrien.SH SYNOPSIS 1685587Sobrien.B awk 1785587Sobrien[ 1885587Sobrien.BI \-F 1985587Sobrien.I fs 2085587Sobrien] 2185587Sobrien[ 2285587Sobrien.BI \-v 2385587Sobrien.I var=value 2485587Sobrien] 2585587Sobrien[ 2685587Sobrien.I 'prog' 2785587Sobrien| 2885587Sobrien.BI \-f 2985587Sobrien.I progfile 3085587Sobrien] 3185587Sobrien[ 3285587Sobrien.I file ... 3385587Sobrien] 3485587Sobrien.SH DESCRIPTION 3585587Sobrien.I Awk 3685587Sobrienscans each input 3785587Sobrien.I file 3885587Sobrienfor lines that match any of a set of patterns specified literally in 3985587Sobrien.IR prog 4085587Sobrienor in one or more files 4185587Sobrienspecified as 4285587Sobrien.B \-f 4385587Sobrien.IR progfile . 4485587SobrienWith each pattern 4585587Sobrienthere can be an associated action that will be performed 4685587Sobrienwhen a line of a 4785587Sobrien.I file 4885587Sobrienmatches the pattern. 4985587SobrienEach line is matched against the 5085587Sobrienpattern portion of every pattern-action statement; 5185587Sobrienthe associated action is performed for each matched pattern. 5285587SobrienThe file name 5385587Sobrien.B \- 5485587Sobrienmeans the standard input. 5585587SobrienAny 5685587Sobrien.IR file 5785587Sobrienof the form 5885587Sobrien.I var=value 5985587Sobrienis treated as an assignment, not a filename, 6085587Sobrienand is executed at the time it would have been opened if it were a filename. 6185587SobrienThe option 6285587Sobrien.B \-v 6385587Sobrienfollowed by 6485587Sobrien.I var=value 6585587Sobrienis an assignment to be done before 6685587Sobrien.I prog 6785587Sobrienis executed; 6885587Sobrienany number of 6985587Sobrien.B \-v 7085587Sobrienoptions may be present. 7185587SobrienThe 7285587Sobrien.B \-F 7385587Sobrien.IR fs 7485587Sobrienoption defines the input field separator to be the regular expression 7585587Sobrien.IR fs. 7685587Sobrien.PP 7785587SobrienAn input line is normally made up of fields separated by white space, 7885587Sobrienor by regular expression 7985587Sobrien.BR FS . 8085587SobrienThe fields are denoted 8185587Sobrien.BR $1 , 8285587Sobrien.BR $2 , 8385587Sobrien\&..., while 8485587Sobrien.B $0 8585587Sobrienrefers to the entire line. 8685587SobrienIf 8785587Sobrien.BR FS 8885587Sobrienis null, the input line is split into one field per character. 8985587Sobrien.PP 9085587SobrienA pattern-action statement has the form 9185587Sobrien.IP 9285587Sobrien.IB pattern " { " action " } 9385587Sobrien.PP 9485587SobrienA missing 9585587Sobrien.BI { " action " } 9685587Sobrienmeans print the line; 9785587Sobriena missing pattern always matches. 9885587SobrienPattern-action statements are separated by newlines or semicolons. 9985587Sobrien.PP 10085587SobrienAn action is a sequence of statements. 10185587SobrienA statement can be one of the following: 10285587Sobrien.PP 10385587Sobrien.EX 10485587Sobrien.ta \w'\f(CWdelete array[expression]'u 10585587Sobrien.RS 10685587Sobrien.nf 10785587Sobrien.ft CW 10885587Sobrienif(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP 10985587Sobrienwhile(\fI expression \fP)\fI statement\fP 11085587Sobrienfor(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP 11185587Sobrienfor(\fI var \fPin\fI array \fP)\fI statement\fP 11285587Sobriendo\fI statement \fPwhile(\fI expression \fP) 11385587Sobrienbreak 11485587Sobriencontinue 11585587Sobrien{\fR [\fP\fI statement ... \fP\fR] \fP} 11685587Sobrien\fIexpression\fP #\fR commonly\fP\fI var = expression\fP 11785587Sobrienprint\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP 11885587Sobrienprintf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP 11985587Sobrienreturn\fR [ \fP\fIexpression \fP\fR]\fP 12085587Sobriennext #\fR skip remaining patterns on this input line\fP 12185587Sobriennextfile #\fR skip rest of this file, open next, start at top\fP 12285587Sobriendelete\fI array\fP[\fI expression \fP] #\fR delete an array element\fP 12385587Sobriendelete\fI array\fP #\fR delete all elements of array\fP 12485587Sobrienexit\fR [ \fP\fIexpression \fP\fR]\fP #\fR exit immediately; status is \fP\fIexpression\fP 12585587Sobrien.fi 12685587Sobrien.RE 12785587Sobrien.EE 12885587Sobrien.DT 12985587Sobrien.PP 13085587SobrienStatements are terminated by 13185587Sobriensemicolons, newlines or right braces. 13285587SobrienAn empty 13385587Sobrien.I expression-list 13485587Sobrienstands for 13585587Sobrien.BR $0 . 13685587SobrienString constants are quoted \&\f(CW"\ "\fR, 13785587Sobrienwith the usual C escapes recognized within. 13885587SobrienExpressions take on string or numeric values as appropriate, 13985587Sobrienand are built using the operators 14085587Sobrien.B + \- * / % ^ 14185587Sobrien(exponentiation), and concatenation (indicated by white space). 14285587SobrienThe operators 14385587Sobrien.B 14485587Sobrien! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?: 14585587Sobrienare also available in expressions. 14685587SobrienVariables may be scalars, array elements 14785587Sobrien(denoted 14885587Sobrien.IB x [ i ] ) 14985587Sobrienor fields. 15085587SobrienVariables are initialized to the null string. 15185587SobrienArray subscripts may be any string, 15285587Sobriennot necessarily numeric; 15385587Sobrienthis allows for a form of associative memory. 15485587SobrienMultiple subscripts such as 15585587Sobrien.B [i,j,k] 15685587Sobrienare permitted; the constituents are concatenated, 15785587Sobrienseparated by the value of 15885587Sobrien.BR SUBSEP . 15985587Sobrien.PP 16085587SobrienThe 16185587Sobrien.B print 16285587Sobrienstatement prints its arguments on the standard output 16385587Sobrien(or on a file if 16485587Sobrien.BI > file 16585587Sobrienor 16685587Sobrien.BI >> file 16785587Sobrienis present or on a pipe if 16885587Sobrien.BI | cmd 16985587Sobrienis present), separated by the current output field separator, 17085587Sobrienand terminated by the output record separator. 17185587Sobrien.I file 17285587Sobrienand 17385587Sobrien.I cmd 17485587Sobrienmay be literal names or parenthesized expressions; 17585587Sobrienidentical string values in different statements denote 17685587Sobrienthe same open file. 17785587SobrienThe 17885587Sobrien.B printf 17985587Sobrienstatement formats its expression list according to the format 18085587Sobrien(see 18185587Sobrien.IR printf (3)) . 18285587SobrienThe built-in function 18385587Sobrien.BI close( expr ) 18485587Sobriencloses the file or pipe 18585587Sobrien.IR expr . 18685587SobrienThe built-in function 18785587Sobrien.BI fflush( expr ) 18885587Sobrienflushes any buffered output for the file or pipe 18985587Sobrien.IR expr . 19085587Sobrien.PP 19185587SobrienThe mathematical functions 19285587Sobrien.BR exp , 19385587Sobrien.BR log , 19485587Sobrien.BR sqrt , 19585587Sobrien.BR sin , 19685587Sobrien.BR cos , 19785587Sobrienand 19885587Sobrien.BR atan2 19985587Sobrienare built in. 20085587SobrienOther built-in functions: 20185587Sobrien.TF length 20285587Sobrien.TP 20385587Sobrien.B length 20485587Sobrienthe length of its argument 20585587Sobrientaken as a string, 20685587Sobrienor of 20785587Sobrien.B $0 20885587Sobrienif no argument. 20985587Sobrien.TP 21085587Sobrien.B rand 211272918Spfgrandom number on [0,1) 21285587Sobrien.TP 21385587Sobrien.B srand 21485587Sobriensets seed for 21585587Sobrien.B rand 21685587Sobrienand returns the previous seed. 21785587Sobrien.TP 21885587Sobrien.B int 21985587Sobrientruncates to an integer value 22085587Sobrien.TP 22185587Sobrien.BI substr( s , " m" , " n\fB) 22285587Sobrienthe 22385587Sobrien.IR n -character 22485587Sobriensubstring of 22585587Sobrien.I s 22685587Sobrienthat begins at position 22785587Sobrien.IR m 22885587Sobriencounted from 1. 22985587Sobrien.TP 23085587Sobrien.BI index( s , " t" ) 23185587Sobrienthe position in 23285587Sobrien.I s 23385587Sobrienwhere the string 23485587Sobrien.I t 23585587Sobrienoccurs, or 0 if it does not. 23685587Sobrien.TP 23785587Sobrien.BI match( s , " r" ) 23885587Sobrienthe position in 23985587Sobrien.I s 24085587Sobrienwhere the regular expression 24185587Sobrien.I r 24285587Sobrienoccurs, or 0 if it does not. 24385587SobrienThe variables 24485587Sobrien.B RSTART 24585587Sobrienand 24685587Sobrien.B RLENGTH 24785587Sobrienare set to the position and length of the matched string. 24885587Sobrien.TP 24985587Sobrien.BI split( s , " a" , " fs\fB) 25085587Sobriensplits the string 25185587Sobrien.I s 25285587Sobrieninto array elements 25385587Sobrien.IB a [1] , 25485587Sobrien.IB a [2] , 25585587Sobrien\&..., 25685587Sobrien.IB a [ n ] , 25785587Sobrienand returns 25885587Sobrien.IR n . 25985587SobrienThe separation is done with the regular expression 26085587Sobrien.I fs 26185587Sobrienor with the field separator 26285587Sobrien.B FS 26385587Sobrienif 26485587Sobrien.I fs 26585587Sobrienis not given. 26685587SobrienAn empty string as field separator splits the string 26785587Sobrieninto one array element per character. 26885587Sobrien.TP 26985587Sobrien.BI sub( r , " t" , " s\fB) 27085587Sobriensubstitutes 27185587Sobrien.I t 27285587Sobrienfor the first occurrence of the regular expression 27385587Sobrien.I r 27485587Sobrienin the string 27585587Sobrien.IR s . 27685587SobrienIf 27785587Sobrien.I s 27885587Sobrienis not given, 27985587Sobrien.B $0 28085587Sobrienis used. 28185587Sobrien.TP 28285587Sobrien.B gsub 28385587Sobriensame as 28485587Sobrien.B sub 28585587Sobrienexcept that all occurrences of the regular expression 28685587Sobrienare replaced; 28785587Sobrien.B sub 28885587Sobrienand 28985587Sobrien.B gsub 29085587Sobrienreturn the number of replacements. 29185587Sobrien.TP 29285587Sobrien.BI sprintf( fmt , " expr" , " ...\fB ) 29385587Sobrienthe string resulting from formatting 29485587Sobrien.I expr ... 29585587Sobrienaccording to the 29685587Sobrien.IR printf (3) 29785587Sobrienformat 29885587Sobrien.I fmt 29985587Sobrien.TP 30085587Sobrien.BI system( cmd ) 30185587Sobrienexecutes 30285587Sobrien.I cmd 30385587Sobrienand returns its exit status 30485587Sobrien.TP 30585587Sobrien.BI tolower( str ) 30685587Sobrienreturns a copy of 30785587Sobrien.I str 30885587Sobrienwith all upper-case characters translated to their 30985587Sobriencorresponding lower-case equivalents. 31085587Sobrien.TP 31185587Sobrien.BI toupper( str ) 31285587Sobrienreturns a copy of 31385587Sobrien.I str 31485587Sobrienwith all lower-case characters translated to their 31585587Sobriencorresponding upper-case equivalents. 31685587Sobrien.PD 31785587Sobrien.PP 31885587SobrienThe ``function'' 31985587Sobrien.B getline 32085587Sobriensets 32185587Sobrien.B $0 32285587Sobriento the next input record from the current input file; 32385587Sobrien.B getline 32485587Sobrien.BI < file 32585587Sobriensets 32685587Sobrien.B $0 32785587Sobriento the next record from 32885587Sobrien.IR file . 32985587Sobrien.B getline 33085587Sobrien.I x 33185587Sobriensets variable 33285587Sobrien.I x 33385587Sobrieninstead. 33485587SobrienFinally, 33585587Sobrien.IB cmd " | getline 33685587Sobrienpipes the output of 33785587Sobrien.I cmd 33885587Sobrieninto 33985587Sobrien.BR getline ; 34085587Sobrieneach call of 34185587Sobrien.B getline 34285587Sobrienreturns the next line of output from 34385587Sobrien.IR cmd . 34485587SobrienIn all cases, 34585587Sobrien.B getline 34685587Sobrienreturns 1 for a successful input, 34785587Sobrien0 for end of file, and \-1 for an error. 34885587Sobrien.PP 34985587SobrienPatterns are arbitrary Boolean combinations 35085587Sobrien(with 35185587Sobrien.BR "! || &&" ) 35285587Sobrienof regular expressions and 35385587Sobrienrelational expressions. 35485587SobrienRegular expressions are as in 35585587Sobrien.IR egrep ; 35685587Sobriensee 35785587Sobrien.IR grep (1). 35885587SobrienIsolated regular expressions 35985587Sobrienin a pattern apply to the entire line. 36085587SobrienRegular expressions may also occur in 36185587Sobrienrelational expressions, using the operators 36285587Sobrien.BR ~ 36385587Sobrienand 36485587Sobrien.BR !~ . 36585587Sobrien.BI / re / 36685587Sobrienis a constant regular expression; 36785587Sobrienany string (constant or variable) may be used 36885587Sobrienas a regular expression, except in the position of an isolated regular expression 36985587Sobrienin a pattern. 37085587Sobrien.PP 37185587SobrienA pattern may consist of two patterns separated by a comma; 37285587Sobrienin this case, the action is performed for all lines 37385587Sobrienfrom an occurrence of the first pattern 37485587Sobrienthough an occurrence of the second. 37585587Sobrien.PP 37685587SobrienA relational expression is one of the following: 37785587Sobrien.IP 37885587Sobrien.I expression matchop regular-expression 37985587Sobrien.br 38085587Sobrien.I expression relop expression 38185587Sobrien.br 38285587Sobrien.IB expression " in " array-name 38385587Sobrien.br 38485587Sobrien.BI ( expr , expr,... ") in " array-name 38585587Sobrien.PP 38685587Sobrienwhere a relop is any of the six relational operators in C, 38785587Sobrienand a matchop is either 38885587Sobrien.B ~ 38985587Sobrien(matches) 39085587Sobrienor 39185587Sobrien.B !~ 39285587Sobrien(does not match). 39385587SobrienA conditional is an arithmetic expression, 39485587Sobriena relational expression, 39585587Sobrienor a Boolean combination 39685587Sobrienof these. 39785587Sobrien.PP 39885587SobrienThe special patterns 39985587Sobrien.B BEGIN 40085587Sobrienand 40185587Sobrien.B END 40285587Sobrienmay be used to capture control before the first input line is read 40385587Sobrienand after the last. 40485587Sobrien.B BEGIN 40585587Sobrienand 40685587Sobrien.B END 40785587Sobriendo not combine with other patterns. 40885587Sobrien.PP 40985587SobrienVariable names with special meanings: 41085587Sobrien.TF FILENAME 41185587Sobrien.TP 41285587Sobrien.B CONVFMT 41385587Sobrienconversion format used when converting numbers 41485587Sobrien(default 41585587Sobrien.BR "%.6g" ) 41685587Sobrien.TP 41785587Sobrien.B FS 41885587Sobrienregular expression used to separate fields; also settable 41985587Sobrienby option 42085587Sobrien.BI \-F fs. 42185587Sobrien.TP 42285587Sobrien.BR NF 42385587Sobriennumber of fields in the current record 42485587Sobrien.TP 42585587Sobrien.B NR 42685587Sobrienordinal number of the current record 42785587Sobrien.TP 42885587Sobrien.B FNR 42985587Sobrienordinal number of the current record in the current file 43085587Sobrien.TP 43185587Sobrien.B FILENAME 43285587Sobrienthe name of the current input file 43385587Sobrien.TP 43485587Sobrien.B RS 43585587Sobrieninput record separator (default newline) 43685587Sobrien.TP 43785587Sobrien.B OFS 43885587Sobrienoutput field separator (default blank) 43985587Sobrien.TP 44085587Sobrien.B ORS 44185587Sobrienoutput record separator (default newline) 44285587Sobrien.TP 44385587Sobrien.B OFMT 44485587Sobrienoutput format for numbers (default 44585587Sobrien.BR "%.6g" ) 44685587Sobrien.TP 44785587Sobrien.B SUBSEP 44885587Sobrienseparates multiple subscripts (default 034) 44985587Sobrien.TP 45085587Sobrien.B ARGC 45185587Sobrienargument count, assignable 45285587Sobrien.TP 45385587Sobrien.B ARGV 45485587Sobrienargument array, assignable; 45585587Sobriennon-null members are taken as filenames 45685587Sobrien.TP 45785587Sobrien.B ENVIRON 45885587Sobrienarray of environment variables; subscripts are names. 45985587Sobrien.PD 46085587Sobrien.PP 46185587SobrienFunctions may be defined (at the position of a pattern-action statement) thus: 46285587Sobrien.IP 46385587Sobrien.B 46485587Sobrienfunction foo(a, b, c) { ...; return x } 46585587Sobrien.PP 46685587SobrienParameters are passed by value if scalar and by reference if array name; 46785587Sobrienfunctions may be called recursively. 46885587SobrienParameters are local to the function; all other variables are global. 46985587SobrienThus local variables may be created by providing excess parameters in 47085587Sobrienthe function definition. 47185587Sobrien.SH EXAMPLES 47285587Sobrien.TP 47385587Sobrien.EX 47485587Sobrienlength($0) > 72 47585587Sobrien.EE 47685587SobrienPrint lines longer than 72 characters. 47785587Sobrien.TP 47885587Sobrien.EX 47985587Sobrien{ print $2, $1 } 48085587Sobrien.EE 48185587SobrienPrint first two fields in opposite order. 48285587Sobrien.PP 48385587Sobrien.EX 48485587SobrienBEGIN { FS = ",[ \et]*|[ \et]+" } 48585587Sobrien { print $2, $1 } 48685587Sobrien.EE 48785587Sobrien.ns 48885587Sobrien.IP 48985587SobrienSame, with input fields separated by comma and/or blanks and tabs. 49085587Sobrien.PP 49185587Sobrien.EX 49285587Sobrien.nf 49385587Sobrien { s += $1 } 49485587SobrienEND { print "sum is", s, " average is", s/NR } 49585587Sobrien.fi 49685587Sobrien.EE 49785587Sobrien.ns 49885587Sobrien.IP 49985587SobrienAdd up first column, print sum and average. 50085587Sobrien.TP 50185587Sobrien.EX 50285587Sobrien/start/, /stop/ 50385587Sobrien.EE 50485587SobrienPrint all lines between start/stop pairs. 50585587Sobrien.PP 50685587Sobrien.EX 50785587Sobrien.nf 50885587SobrienBEGIN { # Simulate echo(1) 50985587Sobrien for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i] 51085587Sobrien printf "\en" 51185587Sobrien exit } 51285587Sobrien.fi 51385587Sobrien.EE 51485587Sobrien.SH SEE ALSO 51585587Sobrien.IR lex (1), 51685587Sobrien.IR sed (1) 51785587Sobrien.br 51885587SobrienA. V. Aho, B. W. Kernighan, P. J. Weinberger, 51985587Sobrien.I 52085587SobrienThe AWK Programming Language, 52185587SobrienAddison-Wesley, 1988. ISBN 0-201-07981-X 52285587Sobrien.SH BUGS 52385587SobrienThere are no explicit conversions between numbers and strings. 52485587SobrienTo force an expression to be treated as a number add 0 to it; 52585587Sobriento force it to be treated as a string concatenate 52685587Sobrien\&\f(CW""\fP to it. 52785587Sobrien.br 52885587SobrienThe scope rules for variables in functions are a botch; 52985587Sobrienthe syntax is worse. 530