📄 _chapter 7.htm
字号:
<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 7</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 6.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 8.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 7. The <span class="docEmphasis">awk</span>
Utility: <span class="docEmphasis">awk</span> Programming</h2><ul><li> <a class="docLink" href="#ch07lev1sec1">7.1 Variables</a></li>
<li> <a class="docLink" href="#ch07lev1sec2">7.2 Redirection and Pipes</a></li>
<li> <a class="docLink" href="#ch07lev1sec3">7.3 Pipes</a></li>
<li> <a class="docLink" href="#ch07lev1sec4">7.4 Closing Files and Pipes</a></li>
<li> <a class="docLink" href="#ch07lev1sec5">7.5 Review</a></li>
<li> <a class="docLink" href="#ch07lev1sec6">UNIX TOOLS LAB EXERCISE</a></li>
<li> <a class="docLink" href="#ch07lev1sec7">7.6 Conditional Statements</a></li>
<li> <a class="docLink" href="#ch07lev1sec8">7.7 Loops</a></li>
<li> <a class="docLink" href="#ch07lev1sec9">7.8 Program Control Statements</a></li>
<li> <a class="docLink" href="#ch07lev1sec10">7.9 Arrays</a></li>
<li> <a class="docLink" href="#ch07lev1sec11">7.10 <span class="docEmphasis">awk</span> Built-In Functions</a></li>
<li> <a class="docLink" href="#ch07lev1sec12">7.11 Built-In Arithmetic Functions</a></li>
<li> <a class="docLink" href="#ch07lev1sec13">7.12 User-Defined Functions<span class="docEmphasis">(nawk)</span></a></li>
<li> <a class="docLink" href="#ch07lev1sec14">7.13 Review</a></li>
<li> <a class="docLink" href="#ch07lev1sec15">UNIX TOOLS LAB EXERCISE</a></li>
<li> <a class="docLink" href="#ch07lev1sec16">7.14 Odds and Ends</a></li>
<li> <a class="docLink" href="#ch07lev1sec17">7.15 Review</a></li>
<li> <a class="docLink" href="#ch07lev1sec18">UNIX TOOLS LAB EXERCISE</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch07.gif" src="ch07.gif" border="0" width="500" height="553"></p>
<h3 class="docSection1Title" id="ch07lev1sec1">7.1 Variables</h3>
<h4 class="docSection2Title" id="ch07lev2sec1">7.1.1 Numeric and String Constants</h4>
<p class="docText">Numeric constants can be represented as integers, such as
243, floating point numbers, such as 3.14, or numbers using scientific notation,
such as .723E-1 or 3.4e7. Strings, such as <span class="docEmphasis">Hello
world,</span> are enclosed in double quotes.</p>
<p class="docText"><b>Initialization and Type Coercion.</b> Just mentioning a
variable in your <span class="docEmphasis">awk</span> program causes it to
exist. A variable can be a string, a number, or both. When it is set, it becomes
the type of the expression on the right-hand side of the equal sign.</p>
<p class="docText">Uninitialized variables have the value zero or the value " ",
depending on the context in which they are used.</p>
<pre>name = "Nancy" <span class="docEmphasis">name is a string</span>
x++ <span class="docEmphBoldItalic">x</span> <span class="docEmphasis">is a number;</span>
<span class="docEmphBoldItalic">x</span> <span class="docEmphasis">is initialized to zero and incremented by 1</span>
number = 35 <span class="docEmphasis">number is a number</span>
</pre>
<p class="docText">To coerce a string to be a number:</p>
<pre>name + 0
</pre>
<p class="docText">To coerce a number to be a string:</p>
<pre>number " "
</pre>
<p class="docText">All fields and array elements created by the
<span class="docEmphasis">split</span> function are considered strings, unless
they contain only a numeric value. If a field or array element is null, it has
the string value of null. An empty line is also considered to be a null string.</p>
<h4 class="docSection2Title" id="ch07lev2sec2">7.1.2 User-Defined Variables</h4>
<p class="docText">User-defined variables consist of letters, digits, and
underscores, and cannot begin with a digit. Variables in
<span class="docEmphasis">awk</span> are not declared. <span class="docEmphasis">
Awk</span> infers data type by the context of the variable in the expression. If
the variable is not initialized, <span class="docEmphasis">awk</span>
initializes string variables to null and numeric variables to zero. If
necessary, <span class="docEmphasis">awk</span> will convert a string variable
to a numeric variable, and vice versa. Variables are assigned values with
<span class="docEmphasis">awk</span>'s assignment operators. See
<a class="docLink" href="#ch07table01">Table 7.1</a>.</p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
<caption>
<h5 id="ch07table01" class="docTableTitle">Table 7.1. Assignment Operators</h5>
</caption>
<colgroup span="3" align="left">
</colgroup>
<tr>
<th class="docTableHeader" vAlign="center"><span class="docEmphBoldItalic">
Operator</span> </th>
<th class="docTableHeader" vAlign="center"><span class="docEmphBoldItalic">
Meaning</span> </th>
<th class="docTableHeader" vAlign="center"><span class="docEmphBoldItalic">
Equivalence</span> </th>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">=</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = 5</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = 5</span>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">+=</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = a + 5</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a += 5</span>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">-=</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = a - 5</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a -= 5</span>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">*=</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = a * 5</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a *= 5</span>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">/=</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = a / 5</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a /= 5</span>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">%=</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = a % 5</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a %= 5</span>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">^=</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a = a ^ 5</span>
</td>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a ^= 5</span>
</td>
</tr>
</table>
<p class="docText">The simplest assignment takes the result of an expression and
assigns it to a variable.</p>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">FORMAT</h2>
<pre>variable = expression
</pre>
</td>
</tr>
</table>
<h5 id="ch07list01" class="docExampleTitle">Example 7.1 </h5>
<pre>% <span class="docEmphStrong">nawk '$1 ~ /Tom/ {wage = $2 * $3; print wage}' 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 scan the first
field for <span class="docEmphasis">Tom</span> and when there is a match, it
will multiply the value of the second field by the value of the third field
and assign the result to the user-defined variable <span class="docEmphasis">
wage.</span> Since the multiplication operation is arithmetic,
<span class="docEmphasis">awk</span> assigns <span class="docEmphasis">wage</span>
an initial value of zero. (The <span class="docEmphasis">%</span> is the
UNIX prompt and <span class="docEmphasis">filename</span> is an input file.)</td>
</tr>
</table>
<p class="docText"><b>Increment and Decrement Operators.</b> To add one to an
operand, the <span class="docEmphasis">increment operator</span> is used. The
expression <span class="docEmphasis">x++</span> is equivalent to
<span class="docEmphasis">x = x + 1.</span> Similarly, the
<span class="docEmphasis">decrement operator</span> subtracts one from its
operand. The expression <span class="docEmphasis">x- -</span> is equivalent to
<span class="docEmphasis">x = x - 1.</span> This notation is useful in looping
operations when you simply want to increment or decrement a counter. You can use
the increment and decrement operators either preceding the operator, as in
<span class="docEmphasis">++</span>x, or after the operator, as in
<span class="docEmphasis">x++.</span> If these expressions are used in
assignment statements, their placement will make a difference in the result of
the operation.</p>
<pre>{x = 1; y = x++ ; print x, y}
</pre>
<p class="docText">The <span class="docEmphasis">++</span> here is called a
<span class="docEmphasis">post-increment operator;</span>
<span class="docEmphasis">y</span> is assigned the value of one, and then
<span class="docEmphasis">x</span> is increased by one, so that when all is said
and done, <span class="docEmphStrong">y</span> <span class="docEmphBoldItalic">
will equal one, and</span> <span class="docEmphStrong">x</span>
<span class="docEmphBoldItalic">will equal two.</span></p>
<pre>{x = 1; y = ++x; print x, y}
</pre>
<p class="docText">The <span class="docEmphasis">++</span> here is called a
<span class="docEmphasis">pre-increment operator;</span>
<span class="docEmphasis">x</span> is incremented first, and the value of two is
assigned to <span class="docEmphasis">y,</span> so that when this statement is
finished, <span class="docEmphStrong">y</span> <span class="docEmphBoldItalic">
will equal two, and</span> <span class="docEmphStrong">x</span>
<span class="docEmphBoldItalic">will equal two.</span></p>
<p class="docText"><b>User-Defined Variables at the Command Line.</b> A variable
can be assigned a value at the command line and passed into an
<span class="docEmphasis">awk</span> script. For more on processing arguments,
see "<a class="docLink" href="#ch07lev2sec25">Processing
Command Arguments (nawk)</a>", <span class="docEmphasis">ARGV,</span> the array
of command line arguments.</p>
<h5 id="ch07list02" class="docExampleTitle">Example 7.2 </h5>
<pre><span class="docEmphStrong">nawk 朏: 杅 awkscript month=4 year=2001 filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -