📄 ch11_10.htm
字号:
as well asthe array membership operator <tt class="literal">in</tt>,andthe pattern-matching operators <tt class="literal">~</tt>and <tt class="literal">!~</tt>(e.g., <tt class="literal">if ($1 ~ /[Aa].*/)</tt>).A series of statements must be put within braces.Another <tt class="literal">if</tt> can directly follow an <tt class="literal">else</tt>in order to produce a chain of tests or decisions.</p></td></tr><tr><td valign="top"><a name="index">index</a></td><td><p><tt class="literal">index(</tt><em class="replaceable"><tt>str</em><tt class="literal">,</tt> <em class="replaceable">substr</tt></em><tt class="literal">)</tt></p><p><a name="IXT-11-123327" />Return the position (starting at 1) of <em class="emphasis">substr</em>in <em class="emphasis">str</em>,or zero if <em class="emphasis">substr</em> is not present in <em class="emphasis">str</em>.</p></td></tr><tr><td valign="top"><a name="int">int</a></td><td><p><tt class="literal">int(</tt><em class="replaceable"><tt>x</tt></em><tt class="literal">)</tt></p><p><a name="IXT-11-123328" />Return integer value of <em class="emphasis">x</em> by truncating anyfractional part.</p></td></tr><tr><td valign="top"><a name="length">length</a></td><td><p><tt class="literal">length(</tt>[<em class="replaceable"><tt>arg</tt></em>]<tt class="literal">)</tt></p><p><a name="IXT-11-123329" />Return length of <em class="emphasis">arg</em>,or the length of <tt class="literal">$0</tt> if no argument.</p></td></tr><tr><td valign="top"><a name="log">log</a></td><td><p><tt class="literal">log(</tt><em class="replaceable"><tt>x</tt></em><tt class="literal">)</tt></p><p><a name="IXT-11-123330" />Return the natural logarithm (base <em class="emphasis">e</em>)of <em class="emphasis">x</em>.</p></td></tr><tr><td valign="top"><a name="match">match</a></td><td><p><tt class="literal">match(</tt><em class="replaceable"><tt>s</em><tt class="literal">,</tt> <em class="replaceable">r</tt></em><tt class="literal">)</tt></p><p><a name="IXT-11-123331" />Function that matches the pattern, specified by the regular expression <em class="emphasis">r</em>,in the string <em class="emphasis">s</em>, andreturns either the position in <em class="emphasis">s</em>, where the matchbegins, or 0 if no occurrences are found. Sets the valuesof <tt class="literal">RSTART</tt> and <tt class="literal">RLENGTH</tt> to the start andlength of the match,respectively.{N}</p></td></tr><tr><td valign="top"><a name="next">next</a></td><td><p><tt class="literal">next</tt></p><p><a name="IXT-11-123332" />Read next input line and start new cycle through pattern/proceduresstatements.</p></td></tr><tr><td valign="top"><a name="nextfile">nextfile</a></td><td><p><tt class="literal">nextfile</tt></p><p><a name="IXT-11-123333" />Stop processing the current input file andstart new cycle through pattern/proceduresstatements,beginning with the first record of the next file.{B} {G}</p></td></tr><tr><td valign="top"><a name="print">print</a></td><td><p><tt class="literal">print</tt> [ <em class="replaceable"><tt>output-expr</em>[, ...]] [ <em class="replaceable">dest-expr</tt></em> ]</p><p><a name="IXT-11-123334" />Evaluate the <em class="emphasis">output-expr</em> and direct it tostandard output, followed by the value of <tt class="literal">ORS</tt>.Each comma-separated <em class="emphasis">output-expr</em> isseparated in the output by the value of <tt class="literal">OFS</tt>.With no <em class="emphasis">output-expr</em>, print <tt class="literal">$0</tt>.</p><h4 class="refsect2">Output Redirections</h4><p><em class="emphasis">dest-expr</em> is anoptional expression that directs the output to a file or pipe.</p><dl><dt><tt class="literal">></tt> <tt><em class="replaceable">file</em></tt></dt><dd>Directs the output to a file,overwriting its previous contents.</p></dd><dt><tt class="literal">>></tt> <tt><em class="replaceable">file</em></tt></dt><dd>Appends the output to a file,preserving its previous contents.In both cases, the file iscreated if it does not already exist.</p></dd><dt><tt class="literal">|</tt> <tt><em class="replaceable">command</em></tt></dt><dd>Directs the output as the input to a Unix command.</p></dd></dl><p>Be careful not to mix <tt class="literal">></tt> and <tt class="literal">>></tt>for the same file.Once a file has been opened with <tt class="literal">></tt>, subsequentoutput statements continue to append to the file until it is closed.</p><p>Remember to call <tt class="literal">close()</tt>when you have finished with a file or pipe.If you don't, eventually you will hit the system limiton the number of simultaneously open files.</p></td></tr><tr><td valign="top"><a name="printf">printf</a></td><td><p><tt class="literal">printf(</tt><tt><em class="replaceable"><tt>format</em></tt> [<tt class="literal">,</tt> <tt><em class="replaceable">expr-list</em></tt> ]<tt class="literal">)</tt> [ <tt><em class="replaceable">dest-expr</tt></em></tt> ]</p><p><a name="IXT-11-123335" />An alternative output statement borrowed from the C language. It can produce formatted output and alsooutput data without automatically producing a newline.<em class="emphasis">format</em></tt> is a string of formatspecifications and constants.<em class="emphasis">expr-list</em></tt> is a list ofarguments corresponding to format specifiers.See <b class="emphasis-bold"><a href="#print">print</a></b> for a description of<em class="emphasis">dest-expr</em></tt>.</p><p><em class="emphasis">format</em></tt> follows the conventions of the C-language<em class="emphasis">printf</em></tt>(3S) library function.Here are a few of the most common formats:</p><table border="1" cellpadding="3"><tr><td><tt class="literal">%s</tt></td><td><p>A string.</p></td></tr><tr><td><tt class="literal">%d</tt></td><td><p>A decimal number.</p></td></tr><tr><td><tt class="literal">%</tt><tt><em class="replaceable"><tt>n</em></tt><tt class="literal">.</tt><tt><em class="replaceable">m</em></tt><tt class="literal">f</tt></td><td><p>A floating-point number; <em class="emphasis">n</em></tt> = total number of digits. <em class="emphasis">m</tt></em></tt> =number of digits after decimal point.</p></td></tr><tr><td><tt class="literal">%[-]</tt><tt><em class="replaceable"><tt>nc</em></tt></td><td><p><em class="emphasis">n</tt></em></tt> specifies minimum field length for format type<em class="emphasis">c</em></tt>, while <tt class="literal">-</tt> left-justifies value infield; otherwise, value is right-justified.</p></td></tr></table><p><p>Like any string,<em class="emphasis">format</em></tt> can also contain embedded escape sequences:<tt class="literal">\n</tt> (newline) or <tt class="literal">\t</tt> (tab)being the most common.Spaces and literal text can be placed in the <em class="emphasis">format</em></tt> argumentby quoting the entire argument.If there are multiple expressions to be printed, there should bemultiple formats specified.</p><h4 class="refsect2">Example</h4><p>Using the script:</p><blockquote><pre class="code">{ printf("The sum on line %d is %.0f.\n", NR, $1+$2) }</pre></blockquote><p>The following input line:</p><blockquote><pre class="code">5 5</pre></blockquote><p>produces this output, followed by a newline:</p><blockquote><pre class="code">The sum on line 1 is 10.</pre></blockquote></td></tr><tr><td valign="top"><a name="rand">rand</a></td><td><p><tt class="literal">rand()</tt></p><p><a name="IXT-11-123336" />Generate a random number between 0 and 1. This function returns thesame series of numbers each time the script is executed, unless the randomnumber generator is seeded using <tt class="literal">srand()</tt>.{N}</p></td></tr><tr><td valign="top"><a name="return">return</a></td><td><p><tt class="literal">return</tt> [<tt><em class="replaceable"><tt>expr</tt></em></tt>]</p><p><a name="IXT-11-123337" />Used within a user-defined function to exit the function,returning value of <em class="emphasis">expr</em></tt>.The return value of a function is undefined if <em class="emphasis">expr</em></tt>is not provided.{N}</p></td></tr><tr><td valign="top"><a name="sin">sin</a></td><td><p><tt class="literal">sin(</tt><tt><em class="replaceable"><tt>x</tt></em></tt><tt class="literal">)</tt></p><p><a name="IXT-11-123338" />Return the sine of <em class="emphasis">x</em></tt>, an angle in radians.{N}</p></td></tr><tr><td valign="top"><a name="split">split</a></td><td><p><tt class="literal">split(</tt><tt><em class="replaceable"><tt>string</em></tt><tt class="literal">,</tt> <tt><em class="replaceable">array</em></tt> [<tt class="literal">,</tt> <tt><em class="replaceable">sep</tt></em></tt>]<tt class="literal">)</tt></p><p><a name="IXT-11-123339" />Split <em class="emphasis">string</em></tt> into elements of array<em class="emphasis">array</em></tt><tt class="literal">[1]</tt>,...,<em class="emphasis">array</em></tt><tt class="literal">[</tt><em class="emphasis">n</em></tt><tt class="literal">]</tt>.The stringis split at each occurrence of separator <em class="emphasis">sep</em></tt>.If <em class="emphasis">sep</em></tt> isnot specified, <tt class="literal">FS</tt> is used.The number of array elements created isreturned.</p></td></tr><tr><td valign="top"><a name="sprintf">sprintf</a></td><td><p><tt class="literal">sprintf(</tt><tt><em class="replaceable"><tt>format</em></tt> [<tt class="literal">,</tt> <tt><em class="replaceable">expressions</tt></em></tt>]<tt class="literal">)</tt></p><p><a name="IXT-11-123340" />Return the formatted value of one or more <em class="emphasis">expressions</em></tt>, using the specified <em class="emphasis">format</em></tt>(see <b class="emphasis-bold"><a href="#printf">printf</a></b>). Data is formatted but not printed.{N}</p></td></tr><tr><td valign="top"><a name="sqrt">sqrt</a></td><td><p><tt class="literal">sqrt(</tt><tt><em class="replaceable"><tt>arg</tt></em></tt><tt class="literal">)</tt></p><p><a name="IXT-11-123341" />Return square root of <em class="emphasis">arg</em></tt>.</p></td></tr><tr><td valign="top"><a name="srand">srand</a></td><td><p><tt class="literal">srand(</tt>[<tt><em class="replaceable"><tt>expr</tt></em></tt>]<tt class="literal">)</tt></p><p><a name="IXT-11-123342" />Use optional <em class="emphasis">expr</em></tt> to set a new seed for therandom number generator.Default is the time of day.Return value is the old seed.{N}</p></td></tr><tr><td valign="top"><a name="strftime">strftime</a></td><td><p><tt class="literal">strftime(</tt>[<tt><em class="replaceable"><tt>format</em></tt> [<tt class="literal">,</tt><tt><em class="replaceable">timestamp</tt></em></tt>]]<tt class="literal">)</tt></p><p><a name="IXT-11-123343" />Format <em class="emphasis">timestamp</em></tt> according to <em class="emphasis">format</em></tt>.Return the formatted string.The <em class="emphasis">timestamp</em></tt> is a time-of-day value in
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -