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

📄 _chapter 6.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 2 页
字号:
  <tr>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Operator</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Meaning</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Example</span> </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">+</span>
    </td>
    <td class="docTableCell" vAlign="top">Add </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">x+y</span>
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">-</span>
    </td>
    <td class="docTableCell" vAlign="top">Subtract </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">x-y</span>
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">*</span>
    </td>
    <td class="docTableCell" vAlign="top">Multiply </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">x*y</span>
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">/</span>
    </td>
    <td class="docTableCell" vAlign="top">Divide </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">x/y</span>
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">%</span>
    </td>
    <td class="docTableCell" vAlign="top">Modulus </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">x%y</span>
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">^</span>
    </td>
    <td class="docTableCell" vAlign="top">Exponentiation </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">x^y</span>
    </td>
  </tr>
</table>
<h5 id="ch06list03" class="docExampleTitle">Example 6.3 </h5>
<pre><span class="docEmphStrong">nawk '$3 * $4 &gt; 500' filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText"><span class="docEmphasis">Awk</span> will multiply the 
    third field (<span class="docEmphasis">$3</span>) by the fourth field (<span class="docEmphasis">$4</span>), 
    and if the result is greater than <span class="docEmphasis">500,</span> it 
    will display those lines. (<span class="docEmphasis">filename</span> is 
    assumed to be a file containing the input.)</td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch06lev2sec4">6.1.4 Compound Patterns</h4>
<p class="docText">Compound patterns are expressions that combine patterns with 
logical operators (see <a class="docLink" href="#ch06table03">Table 6.3</a>). An 
expression is evaluated from left to right.</p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
  <caption>
  <h5 id="ch06table03" class="docTableTitle">Table 6.3. Logical Operators</h5>
  </caption>
  <colgroup span="3" align="left">
  </colgroup>
  <tr>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Operator</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Meaning</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Example</span> </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">&amp;&amp;</span>
    </td>
    <td class="docTableCell" vAlign="top">Logical <span class="docEmphasis">and</span>
    </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">a &amp;&amp; b</span>
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">||</span>
    </td>
    <td class="docTableCell" vAlign="top">Logical <span class="docEmphasis">or</span>
    </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">a || b</span>
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">!</span>
    </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">not</span>
    </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">! a</span>
    </td>
  </tr>
</table>
<h5 id="ch06list04" class="docExampleTitle">Example 6.4 </h5>
<pre><span class="docEmphStrong">nawk '$2 &gt; 5 &amp;&amp; $2 &lt;= 15' filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText"><span class="docEmphasis">Awk</span> will display those 
    lines that match both conditions; that is, where the second field (<span class="docEmphasis">$2</span>) 
    is greater than <span class="docEmphasis">5</span> <span class="docEmphasis">
    and</span> the second field (<span class="docEmphasis">$2</span>) is also 
    less than or equal to <span class="docEmphasis">15.</span> With the
    <span class="docEmphasis">&amp;&amp;</span> operator, <span class="docEmphasis">both</span> 
    conditions must be true. (<span class="docEmphasis">filename</span> is 
    assumed to be a file containing the input.)</td>
  </tr>
</table>
<h5 id="ch06list05" class="docExampleTitle">Example 6.5 </h5>
<pre><span class="docEmphStrong">nawk '$3 == 100 || $4 &gt; 50' filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText"><span class="docEmphasis">Awk</span> will display those 
    lines that match one of the conditions; that is, where the third field is 
    equal to <span class="docEmphasis">100</span> <span class="docEmphasis">or</span> 
    the fourth field is greater than <span class="docEmphasis">50.</span> With 
    the <span class="docEmphasis">||</span> operator, only one of the conditions 
    must be true. (<span class="docEmphasis">filename</span> is assumed to be a 
    file containing the input.)</td>
  </tr>
</table>
<h5 id="ch06list06" class="docExampleTitle">Example 6.6 </h5>
<pre><span class="docEmphStrong">nawk '!($2 &lt; 100 &amp;&amp; $3 &lt; 20)' filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">If both conditions are true, <span class="docEmphasis">
    awk</span> will negate the expression and display those lines. So the lines 
    displayed will have one or both conditions false. The unary
    <span class="docEmphasis">!</span> operator negates the result of the 
    condition so that if the expression yields a true condition, the
    <span class="docEmphasis">not</span> will make it false, and vice versa. (<span class="docEmphasis">filename</span> 
    is assumed to be a file containing the input.)</td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch06lev2sec5">6.1.5 Range Patterns</h4>
<p class="docText">Range patterns match from the first occurrence of one pattern 
to the first occurrence of the second pattern, then match for the next 
occurrence of the first pattern to the next occurrence of the second pattern, 
etc. If the first pattern is matched and the second pattern is not found,
<span class="docEmphasis">awk</span> will display all lines to the end of the 
file.</p>
<h5 id="ch06list07" class="docExampleTitle">Example 6.7 </h5>
<pre><span class="docEmphStrong">nawk '/Tom/,/Suzanne/' filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText"><span class="docEmphasis">Awk</span> will display all 
    lines, inclusive, that range between the first occurrence of
    <span class="docEmphasis">Tom</span> and the first occurrence of
    <span class="docEmphasis">Suzanne.</span> If <span class="docEmphasis">
    Suzanne</span> is not found, <span class="docEmphasis">awk</span> will 
    continue processing lines until the end of file. If, after the range between
    <span class="docEmphasis">Tom</span> and <span class="docEmphasis">Suzanne</span> 
    is printed, <span class="docEmphasis">Tom</span> appears again,
    <span class="docEmphasis">awk</span> will start displaying lines until 
    another <span class="docEmphasis">Suzanne</span> is found or the file ends.</td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch06lev2sec6">6.1.6 A Data Validation Program</h4>
<p class="docText">Using the <span class="docEmphasis">awk</span> commands 
discussed so far, the following password-checking program from the book, <i>The 
AWK Programming Language,</i><span id="ENB6-1"><a class="docLink" href="#EN6-1"><sup>[1]</sup></a></span> 
illustrates how the data in a file can be validated.</p>
<h5 id="ch06list08" class="docExampleTitle">Example 6.8 </h5>
<pre>(The Password Database)
1   % <span class="docEmphStrong">cat /etc/passwd</span>
    tooth:pwHfudo.eC9sM:476:40:Contract Admin.:/home/rickenbacker/tooth:/bin/csh
    lisam:9JY7OuS2f3lHY:4467:40:Lisa M. Spencer:/home/fortune1/lisam:/bin/csh
    goode:v7Ww.nWJCeSIQ:32555:60:Goodwill Guest User:/usr/goodwill:/bin/csh
    bonzo:eTZbu6M2jM7VA:5101:911: SSTOOL Log account :/home/sun4/bonzo:/bin/csh
    info:mKZsrioPtW9hA:611:41:Terri Stern:/home/chewie/info:/bin/csh
    cnc:IN1IVqVj1bVv2:10209:41:Charles Carnell:/home/christine/cnc:/bin/csh
    bee:*:347:40:Contract Temp.:/home/chanel5/bee:/bin/csh
    friedman:oyuIiKoFTV0TE:3561:50:Jay Friedman:/home/ibanez/friedman:/bin/csh
    chambers:Rw7R1k77yUY4.:592:40:Carol Chambers:/usr/callisto2/chambers:/bin/csh
    gregc:nkLulOg:7777:30:Greg Champlin FE Chicago
    ramona:gbDQLdDBeRc46:16660:68:RamonaLeininge MWA CustomerService Rep:/
    home/forsh:

(The <span class="docEmphasis">Awk</span> Commands)
2   % <span class="docEmphStrong">cat /etc/passwd | nawk 朏:</span> '\
3   NF != 7{\
4   printf(&quot;line %d, does not have 7 fields: %s\n&quot;,NR,$0)} \
5   $1 !~ /[A朲a杬0

⌨️ 快捷键说明

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