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

📄 awk.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
If the result is true, thepattern will be considered to match,and the associated action (if any)will be executed.If the result is false, the action will not be executed.<h5><a name = "tag_000_000_108_008">&nbsp;</a>Pattern Ranges</h5>A pattern range consists of two expressionsseparated by a comma; in this case,the action will be performed for all recordsbetween a match of the firstexpression and the following matchof the second expression, inclusive.At this point,the pattern range can be repeated starting at inputrecords subsequentto the end of the matched range.<h5><a name = "tag_000_000_108_009">&nbsp;</a>Actions</h5>An action is a sequence of statementsas shown in the grammar in<xref href=awkgram><a href="#tag_000_000_108_016">Grammar</a></xref>.Any single statement can be replaced bya statement list enclosed in braces.The statements in a statement list must be separated bynewline charactersor semicolons,and will be executed sequentially in the order that they appear.<p>The<i>expression</i>acting as the conditional in an<b>if</b>statement will be evaluated and if it is non-zeroor non-null,the following<i>statement</i>will be executed; otherwise, if<b>else</b>is present, the statement followingthe<b>else</b>will be executed.<p>The<b>if</b>,<b>while</b>,<b>do</b>...<b>while</b>,<b>for</b>,<b>break</b>and<b>continue</b>statements are based on the ISO&nbsp;C standard,except that the Boolean expressions are treated as described in<xref href=awkexpr><a href="#tag_000_000_108_002">Expressions in awk</a></xref>,and except in the case of:<pre><code>for (<i>variable </i>in <i>array</i>)</code></pre>which will iterate, assigning each<i>index</i>of<i>array</i>to<i>variable</i>in an unspecified order.The results of adding new elements to<i>array</i>within such a<b>for</b>loop are undefined.If a<b>break</b>or<b>continue</b>statement occurs outside of a loop, the behaviour is undefined.<p>The<b>delete</b>statement will remove an individual array element.Thus, the following code will delete an entire array:<pre><code>for (index in array)    delete array[index]</code></pre><p>The<b>next</b>statement will cause all furtherprocessing of the current inputrecord to be abandoned.The behaviour is undefined if a<b>next</b>statement appears or is invoked in a<b>BEGIN</b>or<b>END</b>action.<p>The<b>exit</b>statement will invoke all<b>END</b>actions in the order in which they occur in the program sourceand then terminate the programwithout reading further input.An<b>exit</b>statement inside an<b>END</b>action will terminate the programwithout further execution of<b>END</b>actions.If an expression is specified in an<b>exit</b>statement, its numeric value will be the exit status of<i>awk</i>,unless subsequent errors are encountered or a subsequent<b>exit</b>statement with an expression is executed.<h5><a name = "tag_000_000_108_010">&nbsp;</a>Output Statements</h5><xref type="5" name="awkout"></xref>Both<b>print</b>and<b>printf</b>statements write to standard output by default.The output is written to the location specified by<i>output_redirection</i>if one is supplied, as follows:<pre><code>> <i>expression</i>>&gt; <i>expression</i>| <i>expression</i></code></pre><p>In all cases, the<i>expression</i>will be evaluated to produce a string that is used as a full pathnameto write into (for"&gt;" or "&gt;&gt;")or as a command to be executed (for "|").Using the first two forms, if the file of that name is notcurrently open, it will be opened, creating it if necessary and using thefirst form, truncating the file.The output then will be appended to the file.As long as the file remains open,subsequent calls in which<i>expression</i>evaluates to the same string value simply will append output to the file.The file remains open until the<b>close</b>function (see<xref href=awkio><a href="#tag_000_000_108_014">Input/Output and General Functions</a></xref>)is called with an expression that evaluates to the same string value.<p>The third form will write output onto a streampiped to the input 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 be equivalent to one created by a call to the <b>XSH</b> specification<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>w</b>as the<i>mode</i>argument.As long as the stream remains open,subsequent calls in which<i>expression</i>evaluates to the samestring value will write output to the existing stream.The stream will remain open until the<b>close</b>function (see<xref href=awkio><a href="#tag_000_000_108_014">Input/Output and General Functions</a></xref>)is 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 <b>XSH</b> specification<i><a href="../xsh/pclose.html">pclose()</a></i>function.<p>As described in detail by the grammar in<xref href=awkgram><a href="#tag_000_000_108_016">Grammar</a></xref>,these output statements take a comma-separated list of<i>expression</i>sreferred in the grammar by the non-terminal symbols<b>expr_list</b>,<b>print_expr_list</b>or<b>print_expr_list_opt</b>.This list is referred to here as the<i>expression list ,</i>and each member is referred to as an<i>expression argument .</i><p>The<b>print</b>statement will write the value of each expression argumentonto the indicated output stream separated by the current outputfield separator (see variable<b>OFS</b>above), and terminated by the output record separator (see variable<b>ORS</b>above).All expression argumentswill be taken as strings, being converted if necessary;this conversion will be as described in<xref href=awkexpr><a href="#tag_000_000_108_002">Expressions in awk</a></xref>,with the exception that the<b>printf</b>format in<b>OFMT</b>will be used instead of the value in<b>CONVFMT</b>.An empty expression list will stand for the whole input record($0).<p>The<b>printf</b>statement will produce output based on a notation similar to the File FormatNotation used to describe file formats in this specification (seethe <b>XBD</b> specification, <a href="../xbd/notation.html"><b>File Format Notation</b>&nbsp;</a> ).Output will be produced as specified withthe first expression argument as the string&lt;<i>format</i>&gt;and subsequent expression arguments as the strings&lt;<i>arg1</i>&gt;to&lt;<i>argn</i>&gt;,inclusive, with the following exceptions:<ol><p><li>The<i>format</i>will be an actual character string rather than a graphical representation.Therefore, it cannot contain empty character positions.The space character in the<i>format</i>string, in any context other than a<i>flag</i>of a conversion specification,will be treated as an ordinary character that is copied to the output.<p><li>If the character set contains a <img src="../images/delta.gif" border=0> character and that character appearsin the<i>format</i>string, it will be treated as anordinary character that is copied to the output.<p><li>The<i>escape sequences</i>beginning with a backslash character will be treated as sequences ofordinary characters that are copied to the output.Note that these same sequences will be interpreted lexically by<i>awk</i>when they appear in literal strings, but they will not be treated speciallyby the<b>printf</b>statement.<p><li>A<i>field width</i>or<i>precision</i>can be specified as the "*"character instead of a digit string.In this case the next argument from the expression list will be fetchedand its numeric value taken as the field width or precision.<p><li>The implementation will not precede or follow output from thedoruconversion specifications withblank charactersnot specified by the<i>format</i>string.<p><li>The implementation will not precede output from theoconversion specification with leading zeros not specified by the<i>format</i>string.<p><li>For thecconversion specification:if the argument has a numeric value, the character whose encoding is thatvalue will be output.If the value is zero or is not the encoding of any character in thecharacter set, the behaviour is undefined.If the argument does not have a numeric value, the first characterof the string value will be output;if the string does not contain any characters the behaviour is undefined.<p><li>For each conversion specification that consumes an argument,the next expression argument will be evaluated.With the exception of thecconversion, the value will be converted(according to the rules specified in<xref href=awkexpr><a href="#tag_000_000_108_002">Expressions in awk</a></xref>)to the appropriate type for the conversion specification.<p><li>If there are insufficient expression arguments to satisfy all theconversion specifications in the<i>format</i>string, the behaviour is undefined.<p><li>If any character sequence in the<i>format</i>string begins with a % character, but does not form avalid conversion specification, the behaviour is unspecified.<p></ol><p>Both<b>print</b>and<b>printf</b>can output at least{LINE_MAX}bytes.<h5><a name = "tag_000_000_108_011">&nbsp;</a>Functions</h5>The<i>awk</i>languagehas a variety of built-in functions: arithmetic, string, input/outputand general.<h5><a name = "tag_000_000_108_012">&nbsp;</a>Arithmetic Functions</h5>The arithmetic functions, except for<b>int</b>,are based on the ISO&nbsp;C standard.The behaviour is undefined in cases where the ISO&nbsp;C standardspecifies that an error be returned or that the behaviour is undefined.Although the grammar (see<xref href=awkgram><a href="#tag_000_000_108_016">Grammar</a></xref>)permits built-in functions to appearwith no arguments or parentheses, unless the argument orparentheses are indicated as optional in the followinglist (by displaying them within the<b>[&nbsp;]</b>brackets), such use is undefined.<dl compact><dt>atan2(<i>y</i>,<i>x</i>)<dd>Return arctangent of<i>y</i>/<i>x</i>.<dt>cos(<i>x</i>)<dd>Return cosine of<i>x</i>,where<i>x</i>is in radians.<dt>sin(<i>x</i>)<dd>Return sine of<i>x</i>,where<i>x</i>is in radians.<dt>exp(<i>x</i>)<dd>Returnthe exponential function of <i>x</i>.<dt>log(<i>x</i>)<dd>Return the natural logarithm of <i>x</i>.<dt>sqrt(<i>x</i>)<dd>Return the square root of <i>x</i>.<dt>int(<i>x</i>)<dd>Truncate its argument to an integer.It will be truncated toward 0 when<i>x</i>&gt; 0.<dt>rand()<dd>Return a random number<i>n</i>,such that0 &lt;=<i>n</i>&lt; 1.<dt>srand(<b>[</b><i>expr</i><b>]</b>)<dd>Set the seed value for<b>rand</b>to<i>expr</i>or use the time of day if<i>expr</i> is omitted.The previous seed value will be returned.</dl><h5><a name = "tag_000_000_108_013">&nbsp;</a>String Functions</h5><xref type="5" name="awkstr"></xref>The string functions in the following list shall be supported.Although the grammar (see<xref href=awkgram><a href="#tag_000_000_108_016">Grammar</a></xref>)permits built-in functions to appearwith no arguments or parentheses, unless the argument orparentheses are indicated as optional in the followinglist (by displaying them within the<b>[&nbsp;]</b>brackets), such use is undefined.<dl compact><dt>gsub(<i>ere</i>,<i> repl</i><b>[</b>,<i>in</i><b>]</b>)<dd>Behave like<b>sub</b>(see below),except that it will replace all occurrencesof the regular expression(like the<i><a href="ed.html">ed</a></i>utility global substitute) in$0or in the<i>in</i>argument, when specified.<dt>index(<i>s</i>,<i> t</i>)<dd>Return the position, in characters, numbering from 1, in string<i>s</i>where string<i>t</i>first occurs, or zero if it does not occur at all.<dt>length<b>[</b>(<b>[</b><i>s</i><b>]</b>)<b>]</b><dd>Return the length, in characters, of its argument taken as a string,or of the wholerecord,$0,if there is no argument.The use of no argument and no parentheses with<b>length</b>is obsolescent in the ISO/IEC 9945-2:1993 standard;to be fully portable to POSIX systems, the application must uselength($0)for the length of the whole record.However, XSI-conformant systems will continue to supportthis usage indefinitely.<dt>match(<i>s</i>,<i> ere</i>)<dd>Return the position, in characters, numbering from 1, in string<i>s</i>

⌨️ 快捷键说明

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