⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gawk.1

📁 早期freebsd实现
💻 1
📖 第 1 页 / 共 3 页
字号:
.PP.RS.ft B.nfif (val in array)	print array[val].fi.ft.RE.PPIf the array has multiple subscripts, use.BR "(i, j) in array" ..PPThe.B inconstruct may also be used in a.B forloop to iterate over all the elements of an array..PPAn element may be deleted from an array using the.B deletestatement..SS Variable Typing And Conversion.PPVariables and fieldsmay be (floating point) numbers, or strings, or both. How thevalue of a variable is interpreted depends upon its context. If used ina numeric expression, it will be treated as a number, if used as a stringit will be treated as a string..PPTo force a variable to be treated as a number, add 0 to it; to force itto be treated as a string, concatenate it with the null string..PPWhen a string must be converted to a number, the conversion is accomplishedusing.IR atof (3).A number is converted to a string by using the value of.B CONVFMTas a format string for.IR sprintf (3),with the numeric value of the variable as the argument.However, even though all numbers in AWK are floating-point,integral values are.I alwaysconverted as integers.  Thus, given.PP.RS.ft B.nfCONVFMT = "%2.2f"a = 12b = a "".fi.ft R.RE.PPthe variable.B bhas a value of \fB"12"\fR and not \fB"12.00"\fR..PP.I Gawkperforms comparisons as follows:If two variables are numeric, they are compared numerically.If one value is numeric and the other has a string value that is a``numeric string,'' then comparisons are also done numerically.Otherwise, the numeric value is converted to a string and a stringcomparison is performed.Two strings are compared, of course, as strings.According to the \*(PX standard, even if two strings arenumeric strings, a numeric comparison is performed.  However, this isclearly incorrect, and.I gawkdoes not do this..PPUninitialized variables have the numeric value 0 and the string value ""(the null, or empty, string)..SH PATTERNS AND ACTIONSAWK is a line oriented language. The pattern comes first, and then theaction. Action statements are enclosed in.B {and.BR } .Either the pattern may be missing, or the action may be missing, but,of course, not both. If the pattern is missing, the action will beexecuted for every single line of input.A missing action is equivalent to.RS.PP.B "{ print }".RE.PPwhich prints the entire line..PPComments begin with the ``#'' character, and continue until theend of the line.Blank lines may be used to separate statements.Normally, a statement ends with a newline, however, this is not thecase for lines ending ina ``,'', ``{'', ``?'', ``:'', ``&&'', or ``||''.Lines ending in.B door.B elsealso have their statements automatically continued on the following line.In other cases, a line can be continued by ending it with a ``\e'',in which case the newline will be ignored..PPMultiple statements maybe put on one line by separating them with a ``;''.This applies to both the statements within the action part of apattern-action pair (the usual case),and to the pattern-action statements themselves..SS PatternsAWK patterns may be one of the following:.PP.RS.nf.B BEGIN.B END.BI / "regular expression" /.I "relational expression".IB pattern " && " pattern.IB pattern " || " pattern.IB pattern " ? " pattern " : " pattern.BI ( pattern ).BI ! " pattern".IB pattern1 ", " pattern2.fi.RE.PP.B BEGINand.B ENDare two special kinds of patterns which are not tested againstthe input.The action parts of all.B BEGINpatterns are merged as if all the statements hadbeen written in a single.B BEGINblock. They are executed before anyof the input is read. Similarly, all the.B ENDblocks are merged,and executed when all the input is exhausted (or when an.B exitstatement is executed)..B BEGINand.B ENDpatterns cannot be combined with other patterns in pattern expressions..B BEGINand.B ENDpatterns cannot have missing action parts..PPFor.BI / "regular expression" /patterns, the associated statement is executed for each input line that matchesthe regular expression.Regular expressions are the same as those in.IR egrep (1),and are summarized below..PPA.I "relational expression"may use any of the operators defined below in the section on actions.These generally test whether certain fields match certain regular expressions..PPThe.BR && ,.BR || ,and.B !operators are logical AND, logical OR, and logical NOT, respectively, as in C.They do short-circuit evaluation, also as in C, and are used for combiningmore primitive pattern expressions. As in most languages, parenthesesmay be used to change the order of evaluation..PPThe.B ?\^:operator is like the same operator in C. If the first pattern is truethen the pattern used for testing is the second pattern, otherwise it isthe third. Only one of the second and third patterns is evaluated..PPThe .IB pattern1 ", " pattern2form of an expression is called a range pattern.It matches all input records starting with a line that matches.IR pattern1 ,and continuing until a record that matches.IR pattern2 ,inclusive. It does not combine with any other sort of pattern expression..SS Regular ExpressionsRegular expressions are the extended kind found in.IR egrep .They are composed of characters as follows:.TP \w'\fB[^\fIabc...\fB]\fR'u+2n.I cmatches the non-metacharacter.IR c ..TP.I \ecmatches the literal character.IR c ..TP.B .matches any character except newline..TP.B ^matches the beginning of a line or a string..TP.B $matches the end of a line or a string..TP.BI [ abc... ]character class, matches any of the characters.IR abc... ..TP.BI [^ abc... ]negated character class, matches any character except.I abc...and newline..TP.IB r1 | r2alternation: matches either.I r1or.IR r2 ..TP.I r1r2concatenation: matches.IR r1 ,and then.IR r2 ..TP.IB r +matches one or more.IR r 's. .TP.IB r *matches zero or more.IR r 's. .TP.IB r ?matches zero or one.IR r 's. .TP.BI ( r )grouping: matches.IR r ..PPThe escape sequences that are valid in string constants (see below)are also legal in regular expressions..SS ActionsAction statements are enclosed in braces,.B {and.BR } .Action statements consist of the usual assignment, conditional, and loopingstatements found in most languages. The operators, control statements,and input/output statementsavailable are patterned after those in C..SS Operators.PPThe operators in AWK, in order of increasing precedence, are.PP.TP "\w'\fB*= /= %= ^=\fR'u+1n".PD 0.B "= += \-=".TP.PD.B "*= /= %= ^="Assignment. Both absolute assignment.BI ( var " = " value )and operator-assignment (the other forms) are supported..TP.B ?:The C conditional expression. This has the form.IB expr1 " ? " expr2 " : " expr3\c\&. If.I expr1is true, the value of the expression is.IR expr2 ,otherwise it is.IR expr3 .Only one of.I expr2and.I expr3is evaluated..TP.B ||Logical OR..TP.B &&Logical AND..TP.B "~ !~"Regular expression match, negated match..B NOTE:Do not use a constant regular expression.RB ( /foo/ )on the left-hand side of a.B ~or.BR !~ .Only use one on the right-hand side.  The expression.BI "/foo/ ~ " exphas the same meaning as \fB(($0 ~ /foo/) ~ \fIexp\fB)\fR.This is usually.I notwhat was intended..TP.PD 0.B "< >".TP.PD 0.B "<= >=".TP.PD.B "!= =="The regular relational operators..TP.I blankString concatenation..TP.B "+ \-"Addition and subtraction..TP.B "* / %"Multiplication, division, and modulus..TP.B "+ \- !"Unary plus, unary minus, and logical negation..TP.B ^Exponentiation (\fB**\fR may also be used, and \fB**=\fR forthe assignment operator)..TP.B "++ \-\^\-"Increment and decrement, both prefix and postfix..TP.B $Field reference..SS Control Statements.PPThe control statements areas follows:.PP.RS.nf\fBif (\fIcondition\fB) \fIstatement\fR [ \fBelse\fI statement \fR]\fBwhile (\fIcondition\fB) \fIstatement \fR\fBdo \fIstatement \fBwhile (\fIcondition\fB)\fR\fBfor (\fIexpr1\fB; \fIexpr2\fB; \fIexpr3\fB) \fIstatement\fR\fBfor (\fIvar \fBin\fI array\fB) \fIstatement\fR\fBbreak\fR\fBcontinue\fR\fBdelete \fIarray\^\fB[\^\fIindex\^\fB]\fR\fBexit\fR [ \fIexpression\fR ]\fB{ \fIstatements \fB}.fi.RE.SS "I/O Statements".PPThe input/output statements are as follows:.PP.TP "\w'\fBprintf \fIfmt, expr-list\fR'u+1n".BI close( filename )Close file (or pipe, see below)..TP.B getlineSet.B $0from next input record; set.BR NF ,.BR NR ,.BR FNR ..TP.BI "getline <" fileSet.B $0from next record of.IR file ;set.BR NF ..TP.BI getline " var"Set.I varfrom next input record; set.BR NF ,.BR FNR ..TP.BI getline " var" " <" fileSet.I varfrom next record of.IR file ..TP.B nextStop processing the current input record. The next input recordis read and processing starts over with the first pattern in theAWK program. If the end of the input data is reached, the.B ENDblock(s), if any, are executed..TP.B "next file"Stop processing the current input file.  The next input record readcomes from the next input file..B FILENAMEis updated,.B FNRis reset to 1, and processing starts over with the first pattern in theAWK program. If the end of the input data is reached, the.B ENDblock(s), if any, are executed..TP.B printPrints the current record..TP.BI print " expr-list"Prints expressions..TP.BI print " expr-list" " >" filePrints expressions on.IR file ..TP.BI printf " fmt, expr-list"Format and print..TP.BI printf " fmt, expr-list" " >" fileFormat and print on.IR file ..TP.BI system( cmd-line )Execute the command.IR cmd-line ,and return the exit status.(This may not be available on non-\*(PX systems.).PPOther input/output redirections are also allowed. For.B printand.BR printf ,.BI >> fileappends output to the.IR file ,while.BI | " command"writes on a pipe.In a similar fashion,.IB command " | getline"pipes into.BR getline ..BR Getlinewill return 0 on end of file, and \-1 on an error..SS The \fIprintf\fP\^ Statement.PPThe AWK versions of the.B printfstatement and.B sprintf()function(see below)accept the following conversion specification formats:.TP.B %cAn \s-1ASCII\s+1 character.If the argument used for.B %cis numeric, it is treated as a character and printed.Otherwise, the argument is assumed to be a string, and the only firstcharacter of that string is printed..TP.B %dA decimal number (the integer part)..TP.B %iJust like.BR %d ..TP.B %eA floating point number of the form.BR [\-]d.ddddddE[+\^\-]dd ..TP.B %fA floating point number of the form.BR [\-]ddd.dddddd ..TP.B %gUse.B eor.B fconversion, whichever is shorter, with nonsignificant zeros suppressed..TP.B %oAn unsigned octal number (again, an integer)..TP.B %sA character string..TP.B %xAn unsigned hexadecimal number (an integer)..TP.B %XLike.BR %x ,but using.B ABCDEFinstead of.BR abcdef ..TP.B %%A single.B %character; no argument is converted..PPThere are optional, additional parameters that may lie between the.B %and the control letter:.TP.B \-The expression should be left-justified within its field..TP.I widthThe field should be padded to this width. If the number has a leadingzero, then the field will be padded with zeros.Otherwise it is padded with blanks..TP.BI . precA number indicating the maximum width of strings or digits to the rightof the decimal point..PPThe dynamic.I widthand.I preccapabilities of the \*(AN C.B printf()routines are supported.A.B *in place of either the.B widthor.B precspecifications will cause their values to be taken fromthe argument list to.B printfor.BR sprintf() ..SS Special File Names.PPWhen doing I/O redirection from either.B printor.B printfinto a file,or via.B getlinefrom a file,.I gawkrecognizes certain special filenames internally.  These filenamesallow access to open file descriptors inherited from.IR gawk 'sparent process (usually the shell).Other special filenames provide access information about the running.B gawkprocess.The filenames are:.TP \w'\fB/dev/stdout\fR'u+1n.B /dev/pidReading this file returns the process ID of the current process,in decimal, terminated with a newline..TP.B /dev/ppidReading this file returns the parent process ID of the current process,in decimal, terminated with a newline..TP.B /dev/pgrpidReading this file returns the process group ID of the current process,in decimal, terminated with a newline..TP.B /dev/userReading this file returns a single record terminated with a newline.The fields are separated with blanks..B $1is the value of the.IR getuid (2)system call,.B $2is the value of the.IR geteuid (2)system call,.B $3is the value of the.IR getgid (2)system call, and.B $4is the value of the.IR getegid (2)system call.If there are any additional fields, they are the group IDs returned by.IR getgroups (2).(Multiple groups may not be supported on all systems.).TP.B /dev/stdinThe standard input..TP.B /dev/stdoutThe standard output..TP.B /dev/stderrThe standard error output..TP.BI /dev/fd/\^ nThe file associated with the open file descriptor.IR n ..PPThese are particularly useful for error messages. For example:.PP.RS.ft B

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -