📄 ch11_10.htm
字号:
seconds since midnight, January 1, 1970, UTC.The <em class="emphasis">format</em></tt> string is similar to that of<tt class="literal">sprintf</tt>.(See the Example for <b class="emphasis-bold"><a href="#systime">systime</a></b>.)If <em class="emphasis">timestamp</em></tt> is omitted, it defaults to thecurrent time.If <em class="emphasis">format</em></tt> is omitted, it defaults to a valuethat produces output similar to that of <tt class="literal">date</tt>.{G}</p></td></tr><tr><td valign="top"><a name="sub">sub</a></td><td><p><tt class="literal">sub(</tt><tt><em class="replaceable"><tt>r</em></tt><tt class="literal">,</tt> <tt><em class="replaceable">s</em></tt> [<tt class="literal">,</tt> <tt><em class="replaceable">t</tt></em></tt>]<tt class="literal">)</tt></p><p><a name="IXT-11-123344" />Substitute <em class="emphasis">s</em></tt> for first match of theregular expression <em class="emphasis">r</em></tt> in the string <em class="emphasis">t</em></tt>.If <em class="emphasis">t</em></tt> is not supplied,defaults to <tt class="literal">$0</tt>.Return 1 if successful; 0 otherwise.{N}</p></td></tr><tr><td valign="top"><a name="substr">substr</a></td><td><p><tt class="literal">substr(</tt><tt><em class="replaceable"><tt>string</em></tt><tt class="literal">,</tt> <tt><em class="replaceable">beg</em></tt> <tt class="literal">[,</tt> <tt><em class="replaceable">len</tt></em></tt>]<tt class="literal">)</tt></p><p><a name="IXT-11-123345" />Return substring of <em class="emphasis">string</em></tt> at beginningposition <em class="emphasis">beg</em></tt> and the characters thatfollow to maximum specified length <em class="emphasis">len</em></tt>. Ifno length is given, use the rest of the string.</p></td></tr><tr><td valign="top"><a name="system">system</a></td><td><p><tt class="literal">system(</tt><tt><em class="replaceable"><tt>command</tt></em></tt><tt class="literal">)</tt></p><p><a name="IXT-11-123346" />Function that executes the specified<em class="emphasis">command</em></tt> and returns its status. The statusof the executed command typically indicates success or failure. Avalue of 0 means that the command executed successfully. A nonzerovalue indicates a failure of some sort.The documentation for the command you're running will give you thedetails.</p><p>The output of the command is <em class="emphasis">not</em></tt> available for processingwithin the <tt class="literal">awk</tt> script.Use <em class="emphasis">command</em></tt> <tt class="literal">| getline</tt>to read the output of a command into the script.{N}</p></td></tr><tr><td valign="top"><a name="systime">systime</a></td><td><p><tt class="literal">systime()</tt></p><p><a name="IXT-11-123347" />Return atime-of-day value inseconds since midnight, January 1, 1970, UTC.{G}</p><h4 class="refsect2">Example</h4><p>Log the start and end times of a data-processing program:</p><blockquote><pre class="code">BEGIN { now = systime() mesg = strftime("Started at %m/%d/%Y %H:%M:%S", now) print mesg}<tt><em class="replaceable"><tt>process data ...</tt></em></tt>END { now = systime() mesg = strftime("Ended at %m/%d/%Y %H:%M:%S", now) print mesg}</pre></blockquote></td></tr><tr><td valign="top"><a name="tolower">tolower</a></td><td><p><tt class="literal">tolower(</tt><tt><em class="replaceable"><tt>str</tt></em></tt><tt class="literal">)</tt></p><p><a name="IXT-11-123348" />Translate all uppercase charactersin <em class="emphasis">str</em></tt> to lowercase and return the new string.<a href="#FOOTNOTE-15">[15]</a>{N}</p><blockquote class="footnote"><a name="FOOTNOTE-15" /><p> [15]Very early versions of <tt class="literal">nawk</tt>don't support <tt class="literal">tolower()</tt> and<tt class="literal">toupper()</tt>. However, they are now part ofthe POSIX specification for <tt class="literal">awk</tt>, and areincluded in the SVR4 <tt class="literal">nawk</tt>.</p></blockquote></td></tr><tr><td valign="top"><a name="toupper">toupper</a></td><td><p><tt class="literal">toupper(</tt><tt><em class="replaceable"><tt>str</tt></em></tt><tt class="literal">)</tt></p><p><a name="IXT-11-123349" />Translate all lowercase charactersin <em class="emphasis">str</em></tt> to uppercase and return the new string.{N}</p></td></tr><tr><td valign="top"><a name="while">while</a></td><td><p><tt class="literal">while (</tt><tt><em class="replaceable"><tt>condition</em></tt><tt class="literal">)</tt><br>    <tt><em class="replaceable">statement</tt></em></tt></p><p><a name="IXT-11-123350" />Do <em class="emphasis">statement</em></tt> while <em class="emphasis">condition</em></tt> is true(see <tt class="literal">if</tt> for adescription of allowable conditions).A series of statements must be put within braces.</p></td></tr></table><p><a name="unut-ch-11-sect-10.1" /><div class="sect2"><h3 class="sect2">11.10.1. printf Formats</h3><p>Format specifiers for <tt class="literal">printf</tt> and <tt class="literal">sprintf</tt>have the following form:</p><blockquote><pre class="code"><tt class="literal">%</tt>[<tt><em class="replaceable"><tt>flag</em></tt>][<tt><em class="replaceable">width</em></tt>][<tt class="literal">.</tt><tt><em class="replaceable">precision</tt></em></tt>]<tt class="literal">letter</tt></pre></blockquote><p>The control letter is required.The format conversion control letters are as follows.</p><table border="1" cellpadding="3"><tr><th><em class="emphasis">Character</em></tt></th><th><em class="emphasis">Description</em></tt></th></tr><tr><td><tt class="literal">c</tt></td><td>ASCII character</td></tr><tr><td><tt class="literal">d</tt></td><td>Decimal integer</td></tr><tr><td><tt class="literal">i</tt></td><td>Decimal integer (added in POSIX)</td></tr><tr><td><tt class="literal">e</tt></td><td>Floating-point format ([-]<em class="emphasis">d</em></tt>.<em class="emphasis">precision</em></tt><tt class="literal">e</tt>[+-]<em class="emphasis">dd</em></tt>)</td></tr><tr><td><tt class="literal">E</tt></td><td>Floating-point format ([-]<em class="emphasis">d</em></tt>.<em class="emphasis">precision</em></tt><tt class="literal">E</tt>[+-]<em class="emphasis">dd</em></tt>)</td></tr><tr><td><tt class="literal">f</tt></td><td>Floating-point format ([-]<em class="emphasis">ddd</em></tt>.<em class="emphasis">precision</em></tt>)</td></tr><tr><td><tt class="literal">g</tt></td><td><tt class="literal">e</tt> or <tt class="literal">f</tt> conversion, whichever is shortest, with trailing zeros removed</td></tr><tr><td><tt class="literal">G</tt></td><td><tt class="literal">E</tt> or <tt class="literal">f</tt> conversion, whichever is shortest, with trailing zeros removed</td></tr><tr><td><tt class="literal">o</tt></td><td>Unsigned octal value</td></tr><tr><td><tt class="literal">s</tt></td><td>String</td></tr><tr><td><tt class="literal">x</tt></td><td>Unsigned hexadecimal number; uses <tt class="literal">a</tt>-<tt class="literal">f</tt> for 10 to 15</td></tr><tr><td><tt class="literal">X</tt></td><td>Unsigned hexadecimal number; uses <tt class="literal">A</tt>-<tt class="literal">F</tt> for 10 to 15</td></tr><tr><td><tt class="literal">%</tt></td><td>Literal <tt class="literal">%</tt></td></tr></table><p><p>The optional <em class="emphasis">flag</em></tt> is one of the following.</p><table border="1" cellpadding="3"><tr><th><em class="emphasis">Character</em></tt></th><th><em class="emphasis">Description</em></tt></th></tr><tr><td><tt class="literal">-</tt></td><td><p>Left-justify the formatted value within the field.</p></td></tr><tr><td><em class="emphasis">space</em></tt></td><td><p>Prefix positive values with a space and negative values with a minus.</p></td></tr><tr><td><tt class="literal">+</tt></td><td><p>Always prefix numeric values with a sign,even if the value is positive.</p></td></tr><tr><td><tt class="literal">#</tt></td><td><p>Use an alternate form:<tt class="literal">%o</tt> has a preceding <tt class="literal">0</tt>;<tt class="literal">%x</tt> and <tt class="literal">%X</tt> are prefixed with<tt class="literal">0x</tt> and <tt class="literal">0X</tt>, respectively;<tt class="literal">%e</tt>, <tt class="literal">%E</tt>, and <tt class="literal">%f</tt>always have a decimal point in the result;and<tt class="literal">%g</tt> and <tt class="literal">%G</tt> do not havetrailing zeros removed.</p></td></tr><tr><td><tt class="literal">0</tt></td><td><p>Pad output with zeros, not spaces.This happens only when the field width is wider than the converted result.</p></td></tr></table><p><p>The optional <em class="emphasis">width</em></tt> is the minimum number of characters tooutput.The result will be padded to this size if it is smaller.The <tt class="literal">0</tt> flag causes padding with zeros; otherwise,padding is with spaces.</p><p>The <em class="emphasis">precision</em></tt> is optional.Its meaning varies by control letter,as shown in this table.</p><table border="1" cellpadding="3"><tr><th><em class="emphasis">Conversion</em></tt></th><th><em class="emphasis">Precision Means</em></tt></th></tr><tr><td><p><tt class="literal">%d</tt>, <tt class="literal">%i</tt>, <tt class="literal">%o</tt></p><p><tt class="literal">%u</tt>, <tt class="literal">%x</tt>, <tt class="literal">%X</tt></p></td><td>The minimum number of digits to print</td></tr><tr><td><tt class="literal">%e</tt>, <tt class="literal">%E</tt>, <tt class="literal">%f</tt></td><td>The number of digits to the right of the decimal point</td></tr><tr><td><tt class="literal">%g</tt>, <tt class="literal">%G</tt></td><td>The maximum number of significant digits</td></tr><tr><td><tt class="literal">%s</tt></td><td>The maximum number of characters to print</td></tr></table><p><a name="IXT-11-123351" /><a name="IXT-11-123352" /><a name="IXT-11-123353" /></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="part3.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">11.9. Implementation Limits</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">III. Text Formatting</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2003</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,88,96" href="../index.htm"><area shape="rect" coords="90,0,165,96" href="../upt/index.htm"><area shape="rect" coords="168,1,253,107" href="../mac/index.htm"><area shape="rect" coords="255,0,335,97" href="../korn/index.htm"><area shape="rect" coords="337,0,415,109" href="../unixnut/index.htm"><area shape="rect" coords="417,0,512,122" href="../sedawk/index.htm"><area shape="rect" coords="514,0,605,105" href="../lunix/index.htm"><area shape="rect" coords="611,2,694,121" href="../vi/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -