📄 awk.1
字号:
.de EX.nf.ft CW...de EE.br.fi.ft 1..awk.TH AWK 1.CT 1 files prog_other.SH NAMEawk \- pattern-directed scanning and processing language.SH SYNOPSIS.B awk[.BI \-F.I fs][.BI \-v.I var=value][.I 'prog'|.BI \-f.I progfile][.I file ...].SH DESCRIPTION.I Awkscans each input.I filefor lines that match any of a set of patterns specified literally in.IR progor in one or more filesspecified as.B \-f.IR progfile .With each patternthere can be an associated action that will be performedwhen a line of a.I filematches the pattern.Each line is matched against thepattern portion of every pattern-action statement;the associated action is performed for each matched pattern.The file name .B \-means the standard input.Any.IR fileof the form.I var=valueis treated as an assignment, not a filename,and is executed at the time it would have been opened if it were a filename.The option.B \-vfollowed by.I var=valueis an assignment to be done before.I progis executed;any number of.B \-voptions may be present.The.B \-F.IR fsoption defines the input field separator to be the regular expression.IR fs..PPAn input line is normally made up of fields separated by white space,or by regular expression.BR FS .The fields are denoted.BR $1 ,.BR $2 ,\&..., while.B $0refers to the entire line.If.BR FSis null, the input line is split into one field per character..PPA pattern-action statement has the form.IP.IB pattern " { " action " }.PPA missing .BI { " action " }means print the line;a missing pattern always matches.Pattern-action statements are separated by newlines or semicolons..PPAn action is a sequence of statements.A statement can be one of the following:.PP.EX.ta \w'\f(CWdelete array[expression]'u.RS.nf.ft CWif(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fPwhile(\fI expression \fP)\fI statement\fPfor(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fPfor(\fI var \fPin\fI array \fP)\fI statement\fPdo\fI statement \fPwhile(\fI expression \fP)breakcontinue{\fR [\fP\fI statement ... \fP\fR] \fP}\fIexpression\fP #\fR commonly\fP\fI var = expression\fPprint\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fPprintf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fPreturn\fR [ \fP\fIexpression \fP\fR]\fPnext #\fR skip remaining patterns on this input line\fPnextfile #\fR skip rest of this file, open next, start at top\fPdelete\fI array\fP[\fI expression \fP] #\fR delete an array element\fPdelete\fI array\fP #\fR delete all elements of array\fPexit\fR [ \fP\fIexpression \fP\fR]\fP #\fR exit immediately; status is \fP\fIexpression\fP.fi.RE.EE.DT.PPStatements are terminated bysemicolons, newlines or right braces.An empty.I expression-liststands for.BR $0 .String constants are quoted \&\f(CW"\ "\fR,with the usual C escapes recognized within.Expressions take on string or numeric values as appropriate,and are built using the operators.B + \- * / % ^(exponentiation), and concatenation (indicated by white space).The operators.B! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?:are also available in expressions.Variables may be scalars, array elements(denoted.IB 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.B [i,j,k]are permitted; the constituents are concatenated,separated by the value of.BR SUBSEP ..PPThe.B printstatement prints its arguments on the standard output(or on a file if.BI > fileor.BI >> fileis present or on a pipe if.BI | cmdis present), separated by the current output field separator,and terminated by the output record separator..I fileand.I cmdmay be literal names or parenthesized expressions;identical string values in different statements denotethe same open file.The.B printfstatement formats its expression list according to the format(see.IR printf (3)) .The built-in function.BI close( expr )closes the file or pipe.IR expr .The built-in function.BI fflush( expr )flushes any buffered output for the file or pipe.IR expr ..PPThe mathematical functions.BR exp ,.BR log ,.BR sqrt ,.BR sin ,.BR cos ,and.BR atan2 are built in.Other built-in functions:.TF length.TP.B lengththe length of its argumenttaken as a string,or of.B $0if no argument..TP.B randrandom number on (0,1).TP.B srandsets seed for.B randand returns the previous seed..TP.B inttruncates to an integer value.TP.BI substr( s , " m" , " n\fB)the.IR n -charactersubstring of.I sthat begins at position.IR m counted from 1..TP.BI index( s , " t" )the position in.I swhere the string.I toccurs, or 0 if it does not..TP.BI match( s , " r" )the position in.I swhere the regular expression.I roccurs, or 0 if it does not.The variables.B RSTARTand.B RLENGTHare set to the position and length of the matched string..TP.BI split( s , " a" , " fs\fB)splits the string.I sinto array elements.IB a [1] ,.IB a [2] ,\&...,.IB a [ n ] ,and returns.IR n .The separation is done with the regular expression.I fsor with the field separator.B FSif.I fsis not given.An empty string as field separator splits the stringinto one array element per character..TP.BI sub( r , " t" , " s\fB)substitutes.I tfor the first occurrence of the regular expression.I rin the string.IR s .If.I sis not given,.B $0is used..TP.B gsubsame as.B subexcept that all occurrences of the regular expressionare replaced;.B suband.B gsubreturn the number of replacements..TP.BI sprintf( fmt , " expr" , " ...\fB )the string resulting from formatting.I expr ...according to the.IR printf (3)format.I fmt.TP.BI system( cmd )executes.I cmdand returns its exit status.TP.BI tolower( str )returns a copy of.I strwith all upper-case characters translated to theircorresponding lower-case equivalents..TP.BI toupper( str )returns a copy of.I strwith all lower-case characters translated to theircorresponding upper-case equivalents..PD.PPThe ``function''.B getlinesets.B $0to the next input record from the current input file;.B getline.BI < filesets.B $0to the next record from.IR file ..B getline.I xsets variable.I xinstead.Finally,.IB cmd " | getlinepipes the output of.I cmdinto.BR getline ;each call of.B getlinereturns the next line of output from.IR cmd .In all cases,.B getlinereturns 1 for a successful input,0 for end of file, and \-1 for an error..PPPatterns are arbitrary Boolean combinations(with.BR "! || &&" )of regular expressions andrelational expressions.Regular expressions are as in.IR egrep ; see.IR grep (1).Isolated regular expressionsin a pattern apply to the entire line.Regular expressions may also occur inrelational expressions, using the operators.BR ~and.BR !~ ..BI / re /is a constant regular expression;any string (constant or variable) may be usedas a regular expression, except in the position of an isolated regular expressionin a pattern..PPA pattern may consist of two patterns separated by a comma;in this case, the action is performed for all linesfrom an occurrence of the first patternthough an occurrence of the second..PPA relational expression is one of the following:.IP.I expression matchop regular-expression.br.I expression relop expression.br.IB expression " in " array-name.br.BI ( expr , expr,... ") in " array-name.PPwhere a relop is any of the six relational operators in C,and a matchop is either.B ~(matches)or.B !~(does not match).A conditional is an arithmetic expression,a relational expression,or a Boolean combinationof these..PPThe special patterns.B BEGINand.B ENDmay be used to capture control before the first input line is readand after the last..B BEGINand.B ENDdo not combine with other patterns..PPVariable names with special meanings:.TF FILENAME.TP.B CONVFMTconversion format used when converting numbers(default.BR "%.6g" ).TP.B FSregular expression used to separate fields; also settableby option.BI \-F fs..TP.BR NFnumber of fields in the current record.TP.B NRordinal number of the current record.TP.B FNRordinal number of the current record in the current file.TP.B FILENAMEthe name of the current input file.TP.B RSinput record separator (default newline).TP.B OFSoutput field separator (default blank).TP.B ORSoutput record separator (default newline).TP.B OFMToutput format for numbers (default.BR "%.6g" ).TP.B SUBSEPseparates multiple subscripts (default 034).TP.B ARGCargument count, assignable.TP.B ARGVargument array, assignable;non-null members are taken as filenames.TP.B ENVIRONarray of environment variables; subscripts are names..PD.PPFunctions may be defined (at the position of a pattern-action statement) thus:.IP.Bfunction foo(a, b, c) { ...; return x }.PPParameters 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 inthe function definition..SH EXAMPLES.TP.EXlength($0) > 72.EEPrint lines longer than 72 characters..TP.EX{ print $2, $1 }.EEPrint first two fields in opposite order..PP.EXBEGIN { FS = ",[ \et]*|[ \et]+" } { print $2, $1 }.EE.ns.IPSame, with input fields separated by comma and/or blanks and tabs..PP.EX.nf { s += $1 }END { print "sum is", s, " average is", s/NR }.fi.EE.ns.IPAdd up first column, print sum and average..TP.EX/start/, /stop/.EEPrint all lines between start/stop pairs..PP.EX.nfBEGIN { # Simulate echo(1) for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i] printf "\en" exit }.fi.EE.SH SEE ALSO.IR lex (1), .IR sed (1).brA. V. Aho, B. W. Kernighan, P. J. Weinberger,.IThe AWK Programming Language,Addison-Wesley, 1988. ISBN 0-201-07981-X.SH BUGSThere 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\&\f(CW""\fP to it..brThe scope rules for variables in functions are a botch;the syntax is worse.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -