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

📄 awk.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
where the extended regular expression<i>ere</i>occurs, or zero if it does not occur at all.RSTART will be set to the starting position (which isthe same as the returned value), zero if no match is found;RLENGTHwill be set to the length of the matched string, -1 if no match is found.<dt>split(<i>s</i>,<i> a</i><b>[</b>,<i>fs</i><b>]</b>)<dd>Split the string<i>s</i>into array elements<i>a [1],</i><i>a [2],</i>...<i>a [ n</i>and return<i>n .</i>The separation will be done with the extended regular expression<i>fs</i>or with the field separator<b>FS</b>if<i>fs</i>is not given.Each array element will have a string value when created.If the string assigned to any array element,with any occurrence of the decimal-point character from thecurrent locale changed to aperiod character,would be considered a<i>numeric string</i>(see<xref href=awkexpr><a href="#tag_000_000_108_002">Expressions in awk</a></xref>),the array element will also have the numeric value of the<i>numeric string .</i>The effect of a null string as the value of<i>fs</i>is unspecified.<dt>sprintf(<i>fmt</i>,<i> expr</i>,<i> expr</i>, ...)<dd>Format the expressions according to the<b>printf</b>format given by<i>fmt</i>and return the resulting string.<dt>sub(<i>ere</i>,<i> repl</i><b>[</b>,<i>in</i><b>]</b>)<dd>Substitute the string<i>repl</i>in place of the first instance ofthe extended regular expression<i>ERE</i>in string<i>in</i>and return the number of substitutions.An ampersand(&amp;)appearing in the string<i>repl</i>will be replaced by the string from<i>in</i>that matches the regular expression.For each occurrence of backslash (\) encountered when scanning thestring<i>repl</i>from beginning to end, the next character istaken literally and loses its special meaning (for example, \&amp; will beinterpreted as a literal ampersand character).Except for &amp; and \,it is unspecified what the special meaning of any such character is.If<i>in</i>is specified and it is not an<i>lvalue</i>(see<xref href=awkexpr><a href="#tag_000_000_108_002">Expressions in awk</a></xref>),the behaviour is undefined.If<i>in</i>is omitted,<i>awk</i>will substitute in the current record($0).<dt>substr(<i>s</i>,<i> m</i><b>[</b>,<i>n</i><b>]</b>)<dd>Return the at most<i>n -character</i>substring of<i>s</i>that begins at position<i>m</i>,numbering from 1.If<i>n</i>is missing,the length of the substring will be limited by the length of the string<i>s .</i><dt>tolower(<i>s</i>)<dd>Return a string based on the string<i>s .</i>Each character in<i>s</i>that is an upper-case letter specified to have a<b>tolower</b>mapping by theLC_CTYPEcategory of the current locale will be replaced in the returned stringby the lower-case letter specified by the mapping.Other characters in<i>s</i>will be unchanged in the returned string.<dt>toupper(<i>s</i>)<dd>Return a string based on the string<i>s .</i>Each character in<i>s</i>that is a lower-case letter specified to have a<b>toupper</b>mapping by theLC_CTYPEcategory of the current locale will be replaced in the returned stringby the upper-case letter specified by the mapping.Other characters in<i>s</i>will be unchanged in the returned string.</dl><p>All of the preceding functions that take<i>ERE</i>as a parameter expect a pattern or a string valued expression thatis a regular expression as defined in<xref href=awkre><a href="#tag_000_000_108_004">Regular Expressions</a></xref>.<h5><a name = "tag_000_000_108_014">&nbsp;</a>Input/Output and General Functions</h5><xref type="5" name="awkio"></xref>The input/output and general functions are:<dl compact><dt>close(<i>expression</i>)<dd>Close the file or pipeopened by a<b>print</b>or<b>printf</b>statement or a call to<b>getline</b>with the same string-valued<i>expression</i>.The limit on the number of open<i>expression</i>arguments is implementation-dependent.If the close was successful, the function will return zero;otherwise, it will return non-zero.<dt>expression | getline [<i>var</i>]<dd>Read a record of input from a stream piped from the output of a command.The stream will be created if no stream is currently open with the value of<i>expression</i>as its command name.The stream created will beequivalent to one created by a call to the<i><a href="../xsh/popen.html">popen()</a></i>function with the value of<i>expression</i>as the<i>command</i>argument and a value of<b>r</b>as the<i>mode</i>argument.As long as the stream remains open,subsequent calls in which<i>expression</i>evaluates to the same string value will read subsequent records from the file.The stream will remain open until the<b>close</b>functionis called with an expression that evaluates to the same string value.At that time, the stream will be closed as if by a call to the<i><a href="../xsh/pclose.html">pclose()</a></i>function.If<i>var</i>is missing,$0and<b>NF</b>will be set;otherwise,<i>var</i>will be set.The<b>getline</b>operator can form ambiguous constructs when thereare unparenthesised operators (including concatenate) to theleft of the "|"(to the beginning of the expression containing<b>getline</b>).In the context of the "$"operator, "|"behaves as if it had a lower precedence than "$".The result of evaluatingother operators is unspecified, and portable applications mustparenthesise properly all such usages.<dt>getline<dd>Set$0to the next input record from the current input file.This form of<b>getline</b>will set the<b>NF</b>,<b>NR</b>and<b>FNR</b>variables.<dt>getline<i> var</i><dd>Set variable<i>var</i>to the next input record from the current input file.This form of<b>getline</b>will set the<b>FNR</b>and<b>NR</b>variables.<dt>getline <b>[</b><i>var</i><b>] </b>&lt; <i>expression</i><dd>Read the next record of input from a named file.The<i>expression</i>will be evaluated to produce a string that is used as a full pathname.If the file of that name is notcurrently open, it will be opened.As long as the stream remains open,subsequent calls in which<i>expression</i>evaluates to the same string value will read subsequent records from the file.The file will remain open until the<b>close</b>function is called with an expression that evaluates to the same string value.If<i>var</i>is missing,$0and<b>NF</b>will be set;otherwise,<i>var</i>will be set.The<b>getline</b>operator can form ambiguous constructs when thereare unparenthesised binary operators (including concatenate)to the right of the"&lt;"(up to the end of the expression containing the<b>getline</b>).The result of evaluating such a constructis unspecified, and portable applications must parenthesise properly allsuch usages.<dt>system(<i>expression</i>)<dd>Execute the command given by<i>expression</i>in a mannerequivalent to the <b>XSH</b> specification<i><a href="../xsh/system.html">system()</a></i>functionand return the exit status of the command.</dl><p>All forms of<b>getline</b>will return 1 for successful input, zerofor end-of-file, and -1 for an error.<p>Where strings are used asthe name of a file or pipeline, the strings must be textually identical.The terminology &quot;same string value&quot; implies that&quot;equivalent strings&quot;, even those that differ only byspace characters,represent different files.<h5><a name = "tag_000_000_108_015">&nbsp;</a>User-defined Functions</h5><xref type="5" name="awkuser"></xref>The<i>awk</i>language also provides user-defined functions.Such functions can be defined as:<pre><code>function <i>name</i>(<i>args</i>,...) {<i> statements</i> }</code></pre><p>A function can be referred to anywhere in an<i>awk</i>program; inparticular,its use can precede its definition.The scope of a function will be global.<p>Function arguments can be either scalars or arrays; the behaviouris undefined if an array name is passed as an argument that thefunction uses as a scalar, or if a scalar expression is passedas an argument that the function uses as an array.Function arguments will be passed by valueif scalar and by reference if array name.Argument names will be local to the function;all other variable names will be global.The same name will not be used as both anargument name and as the name of a function or a special<i>awk</i>variable.The same name must not be used both as a variablename with global scope and as the name of a function.The same name must not be used within the same scope both as a scalarvariable and as an array.<p>The number of parameters in the function definition need notmatch the number of parameters in the function call.Excess formal parameters can be used as local variables.If fewer arguments are supplied in a function call than are in thefunction definition, the extra parameters that are used in thefunction body as scalars will be initialised with a string valueof the null string and a numeric value of zero, and the extraparameters that are used in the function body as arrays will beinitialised as empty arrays.If more arguments are supplied in a function call than are inthe function definition, the behaviour is undefined.<p>When invoking a function,no white space can be placed between the function name and theopening parenthesis.Function calls can be nestedand recursive calls can be made upon functions.Upon return from any nested or recursive function call,the values of all of the calling function's parameterswill be unchanged, except for array parameters passed by reference.The<b>return</b>statement can be used to return a value.If a<b>return</b>statement appears outside of a function definition,the behaviour is undefined.<p>In the function definition,newline charactersare optional before the opening brace and after the closing brace.Function definitions can appear anywhere in the program where a<i>pattern-action</i>pair is allowed.<h5><a name = "tag_000_000_108_016">&nbsp;</a>Grammar</h5><xref type="5" name="awkgram"></xref>The grammar in this section and the lexicalconventions in the following section will together describe the syntax for<i>awk</i>programs.The general conventions for this style of grammar are described in<xref href=grammar></xref>.A valid program can be represented as the non-terminal symbol<i>program</i>in the grammar.This formal syntax takes precedence overthe preceding text syntax description.<pre><code>%token NAME NUMBER STRING ERE%token FUNC_NAME   /* name followed by '(' without white space *//* Keywords  */%token       Begin   End/*          'BEGIN' 'END'                            */%token       Break   Continue   Delete   Do   Else/*          'break' 'continue' 'delete' 'do' 'else'  */%token       Exit   For   Function   If   In/*          'exit' 'for' 'function' 'if' 'in'        */%token       Next   Print   Printf   Return   While/*          'next' 'print' 'printf' 'return' 'while' *//* Reserved function names */%token BUILTIN_FUNC_NAME            /* one token for the following:             * atan2 cos sin exp log sqrt int rand srand             * gsub index length match split sprintf sub             * substr tolower toupper close system             */%token GETLINE            /* Syntactically different from other built-ins *//* Two-character tokens */%token ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN POW_ASSIGN/*     '+='       '-='       '*='       '/='       '%='       '^=' */%token  OR   AND  NO_MATCH   EQ   LE   GE   NE   INCR  DECR  APPEND/*      '||' '&amp;&amp;' '!' '==' '&lt;=' '&gt;=' '!=' '++'  '--'  '&gt;&gt;'   *//* One-character tokens */%token '{' '}' '(' ')' '[' ']' ',' ';' NEWLINE%token '+' '-' '*' '%' '^' '!' '&gt;' '&lt;' '|' '?' ':' '~' '$' '='%start program%%program          : item_list                 | actionless_item_list                 ;item_list        : newline_opt                 | actionless_item_list item terminator                 | item_list            item terminator                 | item_list          action terminator                 ;actionless_item_list : item_list            pattern terminator                 | actionless_item_list pattern terminator                 ;item             : pattern action                 | Function NAME      '(' param_list_opt ')'                       newline_opt action                 | Function FUNC_NAME '(' param_list_opt ')'                       newline_opt action                 ;param_list_opt   : /* empty */                 | param_list                 ;param_list       : NAME                 | param_list ',' NAME                 ;pattern          : Begin                 | End                 | expr                 | expr ',' newline_opt expr                 ;action           : '{' newline_opt                             '}'                 | '{' newline_opt terminated_statement_list   '}'                 | '{' newline_opt unterminated_statement_list '}'                 ;terminator       : terminator ';'                 | terminator NEWLINE                 |            ';'                 |            NEWLINE                 ;terminated_statement_list : terminated_statement                 | terminated_statement_list terminated_statement                 ;unterminated_statement_list : unterminated_statement                 | terminated_statement_list unterminated_statement                 ;terminated_statement : action newline_opt                 | If '(' expr ')' newline_opt terminate

⌨️ 快捷键说明

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