📄 gawk.1
字号:
print "You blew it!" > "/dev/stderr".ft R.RE.PPwhereas you would otherwise have to use.PP.RS.ft Bprint "You blew it!" | "cat 1>&2".ft R.RE.PPThese file names may also be used on the command line to name data files..SS Numeric Functions.PPAWK has the following pre-defined arithmetic functions:.PP.TP \w'\fBsrand(\^\fIexpr\^\fB)\fR'u+1n.BI atan2( y , " x" )returns the arctangent of.I y/xin radians..TP.BI cos( expr )returns the cosine in radians..TP.BI exp( expr )the exponential function..TP.BI int( expr )truncates to integer..TP.BI log( expr )the natural logarithm function..TP.B rand()returns a random number between 0 and 1..TP.BI sin( expr )returns the sine in radians..TP.BI sqrt( expr )the square root function..TP.BI srand( expr )use.I expras a new seed for the random number generator. If no.I expris provided, the time of day will be used.The return value is the previous seed for the randomnumber generator..SS String Functions.PPAWK has the following pre-defined string functions:.PP.TP "\w'\fBsprintf(\^\fIfmt\fB\^, \fIexpr-list\^\fB)\fR'u+1n"\fBgsub(\fIr\fB, \fIs\fB, \fIt\fB)\fRfor each substring matching the regular expression.I rin the string.IR t ,substitute the string.IR s ,and return the number of substitutions.If.I tis not supplied, use.BR $0 ..TP.BI index( s , " t" )returns the index of the string.I tin the string.IR s ,or 0 if.I tis not present..TP.BI length( s )returns the length of the string.IR s ,or the length of.B $0if.I sis not supplied..TP.BI match( s , " r" )returns the position in.I swhere the regular expression.I roccurs, or 0 if.I ris not present, and sets the values of.B RSTARTand.BR RLENGTH ..TP\fBsplit(\fIs\fB, \fIa\fB, \fIr\fB)\fRsplits the string.I sinto the array.I aon the regular expression.IR r ,and returns the number of fields. If.I ris omitted,.B FSis used..TP.BI sprintf( fmt , " expr-list" )prints.I expr-listaccording to.IR fmt ,and returns the resulting string..TP\fBsub(\fIr\fB, \fIs\fB, \fIt\fB)\fRjust like.BR gsub() ,but only the first matching substring is replaced..TP\fBsubstr(\fIs\fB, \fIi\fB, \fIn\fB)\fRreturns the.IR n -charactersubstring of.I sstarting at.IR i .If.I nis omitted, the rest of.I sis used..TP.BI tolower( str )returns a copy of the string.IR str ,with all the upper-case characters in.I strtranslated to their corresponding lower-case counterparts.Non-alphabetic characters are left unchanged..TP.BI toupper( str )returns a copy of the string.IR str ,with all the lower-case characters in.I strtranslated to their corresponding upper-case counterparts.Non-alphabetic characters are left unchanged..SS Time Functions.PPSince one of the primary uses of AWK programs is processing log filesthat contain time stamp information,.I gawkprovides the following two functions for obtaining time stamps andformatting them..PP.TP "\w'\fBsystime()\fR'u+1n".B systime()returns the current time of day as the number of seconds since the Epoch(Midnight UTC, January 1, 1970 on \*(PX systems)..TP\fBstrftime(\fIformat\fR, \fItimestamp\fB)\fRformats.I timestampaccording to the specification in.IR format.The.I timestampshould be of the same form as returned by.BR systime() .If.I timestampis missing, the current time of day is used.See the specification for the.B strftime()function in \*(AN C for the format conversions that areguaranteed to be available.A public-domain version of.IR strftime (3)and a man page for it are shipped with.IR gawk ;if that version was used to build.IR gawk ,then all of the conversions described in that man page are available to.IR gawk..SS String Constants.PPString constants in AWK are sequences of characters enclosedbetween double quotes (\fB"\fR). Within strings, certain.I "escape sequences"are recognized, as in C. These are:.PP.TP \w'\fB\e\^\fIddd\fR'u+1n.B \e\eA literal backslash..TP.B \eaThe ``alert'' character; usually the \s-1ASCII\s+1 \s-1BEL\s+1 character..TP.B \ebbackspace..TP.B \efform-feed..TP.B \ennewline..TP.B \ercarriage return..TP.B \ethorizontal tab..TP.B \evvertical tab..TP.BI \ex "\^hex digits"The character represented by the string of hexadecimal digits followingthe.BR \ex .As in \*(AN C, all following hexadecimal digits are considered part ofthe escape sequence.(This feature should tell us something about language design by committee.)E.g., "\ex1B" is the \s-1ASCII\s+1 \s-1ESC\s+1 (escape) character..TP.BI \e dddThe character represented by the 1-, 2-, or 3-digit sequence of octaldigits. E.g. "\e033" is the \s-1ASCII\s+1 \s-1ESC\s+1 (escape) character..TP.BI \e cThe literal character.IR c\^ ..PPThe escape sequences may also be used inside constant regular expressions(e.g.,.B "/[\ \et\ef\en\er\ev]/"matches whitespace characters)..SH FUNCTIONSFunctions in AWK are defined as follows:.PP.RS\fBfunction \fIname\fB(\fIparameter list\fB) { \fIstatements \fB}\fR.RE.PPFunctions are executed when called from within the action parts of regularpattern-action statements. Actual parameters supplied in the functioncall are used to instantiate the formal parameters declared in the function.Arrays are passed by reference, other variables are passed by value..PPSince functions were not originally part of the AWK language, the provisionfor local variables is rather clumsy: they are declared as extra parametersin the parameter list. The convention is to separate local variables fromreal parameters by extra spaces in the parameter list. For example:.PP.RS.ft B.nffunction f(p, q, a, b) { # a & b are local ..... }/abc/ { ... ; f(1, 2) ; ... }.fi.ft R.RE.PPThe left parenthesis in a function call is requiredto immediately follow the function name,without any intervening white space.This is to avoid a syntactic ambiguity with the concatenation operator.This restriction does not apply to the built-in functions listed above..PPFunctions may call each other and may be recursive.Function parameters used as local variables are initializedto the null string and the number zero upon function invocation..PPThe word.B funcmay be used in place of.BR function ..SH EXAMPLES.nfPrint and sort the login names of all users:.ft B BEGIN { FS = ":" } { print $1 | "sort" }.ft RCount lines in a file:.ft B { nlines++ } END { print nlines }.ft RPrecede each line by its number in the file:.ft B { print FNR, $0 }.ft RConcatenate and line number (a variation on a theme):.ft B { print NR, $0 }.ft R.fi.SH SEE ALSO.IR egrep (1).PP.IR "The AWK Programming Language" ,Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger,Addison-Wesley, 1988. ISBN 0-201-07981-X..PP.IR "The GAWK Manual" ,Edition 0.15, published by the Free Software Foundation, 1993..SH POSIX COMPATIBILITYA primary goal for.I gawkis compatibility with the \*(PX standard, as well as with thelatest version of \*(UX.IR awk .To this end,.I gawkincorporates the following user visiblefeatures which are not described in the AWK book,but are part of.I awkin System V Release 4, and are in the \*(PX standard..PPThe.B \-voption for assigning variables before program execution starts is new.The book indicates that command line variable assignment happens when.I awkwould otherwise open the argument as a file, which is after the.B BEGINblock is executed. However, in earlier implementations, when such anassignment appeared before any file names, the assignment would happen.I beforethe.B BEGINblock was run. Applications came to depend on this ``feature.''When.I awkwas changed to match its documentation, this option was added toaccomodate applications that depended upon the old behavior.(This feature was agreed upon by both the AT&T and GNU developers.).PPThe.B \-Woption for implementation specific features is from the \*(PX standard..PPWhen processing arguments,.I gawkuses the special option ``\fB\-\^\-\fP'' to signal the end ofarguments, and warns about, but otherwise ignores, undefined options..PPThe AWK book does not define the return value of.BR srand() .The System V Release 4 version of \*(UX.I awk(and the \*(PX standard)has it return the seed it was using, to allow keeping trackof random number sequences. Therefore.B srand()in.I gawkalso returns its current seed..PPOther new features are:The use of multiple.B \-foptions (from MKS.IR awk );the.B ENVIRONarray; the.BR \ea ,and.BR \evescape sequences (done originally in.I gawkand fed back into AT&T's version); the.B tolower()and.B toupper()built-in functions (from AT&T); and the \*(AN C conversion specifications in.B printf(done first in AT&T's version)..SH GNU EXTENSIONS.I Gawkhas some extensions to \*(PX.IR awk .They are described in this section. All the extensions described herecan be disabled byinvoking.I gawkwith the.B "\-W compat"option..PPThe following features of.I gawkare not available in\*(PX.IR awk ..RS.TP \w'\(bu'u+1n\(buThe.B \exescape sequence..TP\(buThe.B systime()and.B strftime()functions..TP\(buThe special file names available for I/O redirection are not recognized..TP\(buThe.B ARGINDand.B ERRNOvariables are not special..TP\(buThe.B IGNORECASEvariable and its side-effects are not available..TP\(buThe.B FIELDWIDTHSvariable and fixed width field splitting..TP\(buNo path search is performed for files named via the.B \-foption. Therefore the.B AWKPATHenvironment variable is not special..TP\(buThe use of.B "next file"to abandon processing of the current input file..RE.PPThe AWK book does not define the return value of the.B close()function..IR Gawk\^ 's.B close()returns the value from.IR fclose (3),or.IR pclose (3),when closing a file or pipe, respectively..PPWhen.I gawkis invoked with the.B "\-W compat"option,if the.I fsargument to the.B \-Foption is ``t'', then.B FSwill be set to the tab character.Since this is a rather ugly special case, it is not the default behavior.This behavior also does not occur if.B \-W posixhas been specified..ig.PPIf.I gawkwas compiled for debugging, it willaccept the following additional options:.TP.PD 0.B \-Wparsedebug.TP.PD.B \-\^\-parsedebugTurn on.IR yacc (1)or.IR bison (1)debugging output during program parsing.This option should only be of interest to the.I gawkmaintainers, and may not even be compiled into.IR gawk ....SH HISTORICAL FEATURESThere are two features of historical AWK implementations that.I gawksupports.First, it is possible to call the.B length()built-in function not only with no argument, but even without parentheses!Thus,.RS.PP.ft Ba = length.ft R.RE.PPis the same as either of.RS.PP.ft Ba = length().bra = length($0).ft R.RE.PPThis feature is marked as ``deprecated'' in the \*(PX standard, and.I gawkwill issue a warning about its use if.B \-W lintis specified on the command line..PPThe other feature is the use of the.B continuestatement outside the body of a.BR while ,.BR for ,or.B doloop. Traditional AWK implementations have treated such usage asequivalent to the.B nextstatement..I Gawkwill support this usage if.B \-W posixhas not been specified..SH BUGSThe.B \-Foption is not necessary given the command line variable assignment feature;it remains only for backwards compatibility..PPIf your system actually has support for.B /dev/fdand the associated.BR /dev/stdin ,.BR /dev/stdout ,and.B /dev/stderrfiles, you may get different output from.I gawkthan you would get on a system without those files. When.I gawkinterprets these files internally, it synchronizes output to the standardoutput with output to.BR /dev/stdout ,while on a system with those files, the output is actually to differentopen files.Caveat Emptor..SH VERSION INFORMATIONThis man page documents.IR gawk ,version 2.15..PPStarting with the 2.15 version of.IR gawk ,the.BR \-c ,.BR \-V ,.BR \-C ,.ig.BR \-D ,...BR \-a ,and.B \-eoptions of the 2.11 version are no longer recognized..SH AUTHORSThe original version of \*(UX.I awkwas designed and implemented by Alfred Aho,Peter Weinberger, and Brian Kernighan of AT&T Bell Labs. Brian Kernighancontinues to maintain and enhance it..PPPaul Rubin and Jay Fenlason,of the Free Software Foundation, wrote.IR gawk ,to be compatible with the original version of.I awkdistributed in Seventh Edition \*(UX.John Woods contributed a number of bug fixes.David Trueman, with contributionsfrom Arnold Robbins, made.I gawkcompatible with the new version of \*(UX.IR awk ..PPThe initial DOS port was done by Conrad Kwok and Scott Garfinkle.Scott Deifik is the current DOS maintainer. Pat Rankin did theport to VMS, and Michal Jaegermann did the port to the Atari ST..SH ACKNOWLEDGEMENTSBrian Kernighan of Bell Labsprovided valuable assistance during testing and debugging.We thank him.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -