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

📄 ch11_03.htm

📁 unix基础教程
💻 HTM
字号:
<html><head><title>Patterns and Procedures (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 &amp; 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_02.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_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">11.3. Patterns and Procedures</h2><p><a name="IXT-11-123269" /><a name="IXT-11-123270" /><tt class="literal">awk</tt> scripts consist of patterns and procedures:</p><blockquote><pre class="code"><em class="replaceable"><tt>pattern</em>  { <em class="replaceable">procedure</tt></em> }</pre></blockquote><p>Both are optional.  If <em class="emphasis">pattern</em> is missing, <tt class="literal">{</tt><em class="emphasis">procedure</em> <tt class="literal">}</tt> is applied to all lines; if<tt class="literal">{</tt> <em class="emphasis">procedure</em> <tt class="literal">}</tt> is missing, thematched line is printed.</p><a name="unut-ch-11-sect-3.1" /><div class="sect2"><h3 class="sect2">11.3.1. Patterns</h3><p>A pattern can be any of the following:</p><blockquote><pre class="code">/<em class="replaceable"><tt>regular expression</tt></em>/<em class="replaceable"><tt>relational expression</tt></em><em class="replaceable"><tt>pattern-matching expression</tt></em>BEGINEND</pre></blockquote><ul><li><p>Expressions can be composed of quoted strings, numbers, operators,functions, defined variables, or any of the predefined variables described later in <a href="ch11_04.htm#unut-ch-11-sect-4">Section 11.4</a>.</p></li><li><p>Regular expressions use the extended set of metacharacters andare described in <a href="ch06_01.htm">Chapter 6</a>.</p></li><li><p><tt class="literal">^</tt> and <tt class="literal">$</tt> refer to thebeginning and end of a string (such as the fields), respectively, ratherthan the beginning and end of a line.In particular, these metacharacters will <em class="emphasis">not</em>match at a newline embedded in the middle of a string.</p></li><li><p>Relational expressions use the relational operators listed in <a href="ch11_05.htm#unut-ch-11-sect-5">Section 11.5</a> later in this chapter.For example, <tt class="literal">$2 &gt; $1</tt> selects lines forwhich the second field is greater than the first.  Comparisons can be either string or numeric.Thus, depending on the types of data in <tt class="literal">$1</tt>and <tt class="literal">$2</tt>, <tt class="literal">awk</tt> does eithera numeric or a string comparison.This can change from one record to the next.</p></li><li><p>Pattern-matching expressions use the operators <tt class="literal">~</tt> (match)and <tt class="literal">!~</tt> (don't match).See <a href="ch11_05.htm#unut-ch-11-sect-5">Section 11.5</a> later in this chapter.</p></li><li><p>The <tt class="literal">BEGIN</tt> pattern lets you specify procedures that takeplace <em class="emphasis">before</em> the first input line is processed.(Generally, you set global variables here.)</p></li><li><p>The <tt class="literal">END</tt> pattern lets you specify procedures thattake place <em class="emphasis">after</em> the last input record is read.</p></li><li><p>In <tt class="literal">nawk</tt>,<tt class="literal">BEGIN</tt> and <tt class="literal">END</tt> patternsmay appear multiple times. The procedures are merged as if therehad been one large procedure.</p></li></ul><p>Except for <tt class="literal">BEGIN</tt> and <tt class="literal">END</tt>,patterns can be combined with the Boolean operators <tt class="literal">||</tt> (or), <tt class="literal">&amp;&amp;</tt> (and), and <tt class="literal">!</tt> (not).  A range of lines can also be specified using comma-separated patterns:</p><blockquote><pre class="code"><em class="replaceable"><tt>pattern</em>,<em class="replaceable">pattern</tt></em></pre></blockquote></div><a name="unut-ch-11-sect-3.2" /><div class="sect2"><h3 class="sect2">11.3.2. Procedures</h3><p><a name="IXT-11-123271" />Procedures consist of one or more commands, functions, or variableassignments, separated by newlines or semicolons, and contained withincurly braces.  Commands fall into five groups:</p><ul><li><p>Variable or array assignments</p></li><li><p>Printing commands</p></li><li><p>Built-in functions</p></li><li><p>Control-flow commands</p></li><li><p>User-defined functions (<tt class="literal">nawk</tt> only)</p></li></ul></div><a name="unut-ch-11-sect-3.3" /><div class="sect2"><h3 class="sect2">11.3.3. Simple Pattern-Procedure Examples</h3><ul><li><p>Print first field of each line:</p><blockquote><pre class="code">{ print $1 }</pre></blockquote></li><li><p>Print all lines that contain <em class="emphasis">pattern</em>:</p><blockquote><pre class="code">/<em class="replaceable"><tt>pattern</tt></em>/</pre></blockquote></li><li><p>Print first field of lines that contain <em class="emphasis">pattern</em>:</p><blockquote><pre class="code">/<em class="replaceable"><tt>pattern</tt></em>/ { print $1 }</pre></blockquote></li><li><p>Select records containing more than two fields:</p><blockquote><pre class="code">NF &gt; 2</pre></blockquote></li><li><p>Interpret input records as a group of lines up toa blank line.  Each line is a single field:</p><blockquote><pre class="code">BEGIN { FS = "\n"; RS = "" }</pre></blockquote></li><li><p>Print fields 2 and 3 in switched order, but only on lineswhose first field matches the string &#8220;URGENT&#8221;:</p><blockquote><pre class="code">$1 ~ /URGENT/ { print $3, $2 }</pre></blockquote></li><li><p>Count and print the number of <em class="emphasis">pattern</em> found:</p><blockquote><pre class="code">/<em class="replaceable"><tt>pattern</tt></em>/ { ++x }END { print x }</pre></blockquote></li><li><p>Add numbers in second column and print total:</p><blockquote><pre class="code">{ total += $2 }END { print "column total is", total}</pre></blockquote></li><li><p>Print lines that contain less than 20 characters:</p><blockquote><pre class="code">length($0) &lt; 20</pre></blockquote></li><li><p>Print each line that begins with <em class="emphasis">Name:</em> and that contains exactly seven fields:</p><blockquote><pre class="code">NF == 7 &amp;&amp; /^Name:/</pre></blockquote></li><li><p>Print the fields of each input record in reverse order, one per line:</p><blockquote><pre class="code">{	for (i = NF; i &gt;= 1; i--)		print $i}</pre></blockquote></li></ul></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_02.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_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">11.2. Command-Line Syntax</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.4. Built-in Variables</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 &copy; 2003</a> O'Reilly &amp; 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 + -