?? _chapter 6.htm
字號(hào):
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 6</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body><table width="100%" border="1" bgcolor="#EBEBFF"><tr><td width="5%" align="left" valign="middle"><a href="_chapter 5.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td><td align="center" valign="middle"><a class="docLink" href="Front matter.htm">CONTENTS</a></td><td width="5%" align="right" valign="middle"><a href="_chapter 7.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 6. The <span class="docEmphasis">awk</span>
Utility: <span class="docEmphasis">awk</span> Programming Constructs</h2><ul><li> <a class="docLink" href="#ch06lev1sec1">6.1 Comparison Expressions</a></li>
<li> <a class="docLink" href="#ch06lev1sec2">6.2 Review</a></li>
<li> <a class="docLink" href="#ch06lev1sec3">UNIX TOOLS LAB EXERCISE</a></li>
</ul>
<p><br>
</p>
<p class="docText">
<img alt="graphics/ch06.gif" src="ch06.gif" border="0" width="500" height="613"></p>
<h3 class="docSection1Title" id="ch06lev1sec1">6.1 Comparison Expressions</h3>
<p class="docText">Comparison expressions match lines where if the condition is
true, the action is performed. These expressions use relational operators and
are used to compare numbers or strings. <a class="docLink" href="#ch06table01">
Table 6.1</a> provides a list of the relational operators. The value of the
expression is 1 if the expression evaluates true, and 0 if false.</p>
<h4 class="docSection2Title" id="ch06lev2sec1">6.1.1 Relational Operators</h4>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
<caption>
<h5 id="ch06table01" class="docTableTitle">Table 6.1. Relational 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"><</span>
</td>
<td class="docTableCell" vAlign="top">Less than. </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">Less than or equal to. </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">Equal to. </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">Not equal to. </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">Greater than or equal to. </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">Greater than. </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">Matched by regular expression. </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">Not matched by regular expression.
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">x !~ /y/</span>
</td>
</tr>
</table>
<h5 id="ch06list01" class="docExampleTitle">Example 6.1 </h5>
<pre>(The Database)
% <span class="docEmphStrong">cat employee</span>
<span class="docEmphasis">Tom Jones 4423 5/12/66 543354</span>
<span class="docEmphasis">Mary Adams 5346 11/4/63 28765</span>
<span class="docEmphasis">Sally Chang 1654 7/22/54 650000</span>
<span class="docEmphasis">Billy Black 1683 9/23/44 336500</span>
(The Command Line)
1 % <span class="docEmphStrong">nawk '$3 == 5346' employees</span>
<span class="docEmphasis">Mary Adams 5346 11/4/63 28765</span>
2 % <span class="docEmphStrong">nawk '$3 > 5000{print $1} ' employees</span>
<span class="docEmphasis">Mary</span>
3 % <span class="docEmphStrong">nawk '$2 ~ /Adam/ ' employees</span>
<span class="docEmphasis">Mary Adams 5346 11/4/63 28765</span>
4 % <span class="docEmphStrong">nawk '$2 !~ /Adam/ ' employees</span>
<span class="docEmphasis">Tom Jones 4423 5/12/66 543354</span>
<span class="docEmphasis">Sally Chang 1654 7/22/54 650000</span>
<span class="docEmphasis">Billy Black 1683 9/23/44 336500</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the third field is equal to
<span class="docEmphasis">5346,</span> the condition is true and
<span class="docEmphasis">awk</span> will perform the default action梡rint
the line. When an <span class="docEmphasis">if</span> condition is
implied, it is a conditional pattern test.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the third field is greater than
<span class="docEmphasis">5000,</span> <span class="docEmphasis">awk</span>
prints the first field.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the second field matches the regular expression
<span class="docEmphasis">Adam,</span> the record is printed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the second field does not match the regular
expression <span class="docEmphasis">Adam,</span> the record is printed.
If an expression is a numeric value and is being compared to a string
value with an operator that requires a numeric comparison, the string
value will be converted to a numeric value. If the operator requires a
string value, the numeric value will be converted to a string value.</span></li>
</ol>
</span></td>
</tr>
</table>
<h4 class="docSection2Title" id="ch06lev2sec2">6.1.2 Conditional Expressions</h4>
<p class="docText">A conditional expression uses two symbols, the question mark
and the colon, to evaluate expressions. It is really just a short way to achieve
the same result as doing an <span class="docEmphasis">if/else</span> statement.
The general format is shown below.</p>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">FORMAT</h2>
<pre>conditional expression1 ? expression2 : expression3
</pre>
</td>
</tr>
</table>
<p class="docText">This produces the same result as the
<span class="docEmphasis">if/else</span> shown below. (A complete discussion of
the <span class="docEmphasis">if/else</span> construct is given later.)</p>
<pre>{
if (expression1)
expression2
else
expression3
}
</pre>
<h5 id="ch06list02" class="docExampleTitle">Example 6.2 </h5>
<pre><span class="docEmphStrong">nawk '{max=($1 > $2) ? $1 : $2; print max}' filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">If the first field is greater than the second field, the
value of the expression after the question mark is assigned to
<span class="docEmphasis">max,</span> otherwise the value of the expression
after the colon is assigned to <span class="docEmphasis">max.</span></p>
<p class="docText">This is comparable to</p>
<pre>if ($1 > $2)
max=$1
else
max=$2
</pre>
</td>
</tr>
</table>
<h4 class="docSection2Title" id="ch06lev2sec3">6.1.3 Computation</h4>
<p class="docText">Computation can be performed within patterns.
<span class="docEmphasis">Awk</span> performs all arithmetic in floating point.
The arithmetic operators are provided in <a class="docLink" href="#ch06table02">
Table 6.2</a>.</p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
<caption>
<h5 id="ch06table02" class="docTableTitle">Table 6.2. Arithmetic Operators</h5>
</caption>
<colgroup span="3" align="left">
</colgroup>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -