?? ch11_02.htm
字號:
<html><head><title>Command-Line Syntax (UNIX in a Nutshell: System V Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Arnold Robbins" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="1-56592-427-4" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="UNIX in a Nutshell: System V Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Book Title" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch11_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">11.2. Command-Line Syntax</h2><p><a name="IXT-11-123263" />The syntax for invoking <tt class="literal">awk</tt> has two forms:</p><blockquote><pre class="code">awk [<em class="replaceable"><tt>options</em>] '<em class="replaceable">script</em>' <em class="replaceable">var</em>=<em class="replaceable">value file(s)</tt></em>awk [<em class="replaceable"><tt>options</em>] -f <em class="replaceable">scriptfile var</em>=<em class="replaceable">value file(s)</tt></em></pre></blockquote><p>You can specify a <em class="emphasis">script</em> directly on the command line,or you can store a script in a <em class="emphasis">scriptfile</em> and specify itwith <tt class="literal">-f</tt>.<tt class="literal">nawk</tt> allows multiple <tt class="literal">-f</tt> scripts.Variables can be assigned a value on thecommand line. The value can be a literal, a shell variable(<tt class="literal">$</tt><em class="replaceable"><tt>name</tt></em>), or a command substitution(<tt class="literal">‘</tt><em class="replaceable"><tt>cmd</tt></em><tt class="literal">‘</tt>),but the value is available onlyafter the <tt class="literal">BEGIN</tt>statement is executed.</p><p><tt class="literal">awk</tt> operates on one or more<em class="emphasis">files</em>. If none are specified (or if <tt class="literal">-</tt>is specified), <tt class="literal">awk</tt> reads from the standard input.<a name="IXT-11-123264" /><a name="IXT-11-123265" /><a name="IXT-11-123266" /><a name="IXT-11-123267" /><a name="IXT-11-123268" /></p><p>The recognized <em class="emphasis">options</em> are:</p><dl><dt><tt class="literal">-F</tt><tt><em class="replaceable">fs</em></tt></dt><dd>Set the field separator to <em class="emphasis">fs</em></tt>. This is the same as setting the system variable <tt class="literal">FS</tt>.Original <tt class="literal">awk</tt> allows the field separator tobe only a single character.<tt class="literal">nawk</tt> allows <em class="emphasis">fs</em></tt> to be a regular expression.Each input line, or record, is divided into fields bywhitespace (blanks or tabs) or by some other user-definable recordseparator.Fields are referred to by the variables <tt class="literal">$1</tt>, <tt class="literal">$2</tt>,..., <tt class="literal">$</tt><tt><em class="replaceable"><tt>n</tt></em></tt>.<tt class="literal">$0</tt> refers to the entire record.</p></dd><dt><tt class="literal">-v</tt> <tt><em class="replaceable">var</em></tt><tt class="literal">=</tt><tt><em class="replaceable">value</em></tt></dt><dd>Assign a <em class="emphasis">value</em></tt> to variable <em class="emphasis">var</em></tt>.This allows assignment before the script begins execution(available in <tt class="literal">nawk</tt> only).</p></dd></dl><p>Toprint the first three (colon-separated) fields of each recordon separate lines:</p><blockquote><pre class="code"><tt class="userinput"><b>awk -F: '{ print $1; print $2; print $3 }' /etc/passwd</b></tt></pre></blockquote><p>More examples are shown in <a href="ch11_03.htm#unut-ch-11-sect-3.3">Section 11.3.3</a>.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_01.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="ch11_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">11. The awk Programming Language</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">11.3. Patterns and Procedures</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>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -