?? _chapter 5.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 5</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 4.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 6.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 5. The <span class="docEmphasis">awk</span>
Utility: <span class="docEmphasis">awk</span> as a UNIX Tool</h2><ul><li> <a class="docLink" href="#ch05lev1sec1">5.1 What Is <span class="docEmphasis">awk?</span></a></li>
<li> <a class="docLink" href="#ch05lev1sec2">5.2 <span class="docEmphasis">awk</span>'s Format</a></li>
<li> <a class="docLink" href="#ch05lev1sec3">5.3 Formatting Output</a></li>
<li> <a class="docLink" href="#ch05lev1sec4">5.4 <span class="docEmphasis">awk</span> Commands from Within a File</a></li>
<li> <a class="docLink" href="#ch05lev1sec5">5.5 Records and Fields</a></li>
<li> <a class="docLink" href="#ch05lev1sec6">5.6 Patterns and Actions</a></li>
<li> <a class="docLink" href="#ch05lev1sec7">5.7 Regular Expressions</a></li>
<li> <a class="docLink" href="#ch05lev1sec8">5.8 <span class="docEmphasis">awk</span> Commands in a Script File</a></li>
<li> <a class="docLink" href="#ch05lev1sec9">5.9 Review</a></li>
<li> <a class="docLink" href="#ch05lev1sec10">UNIX TOOLS LAB EXERCISE</a></li>
</ul>
<p class="docText"><img alt="graphics/ch05.gif" src="ch05.gif" border="0" width="500" height="406"></p>
<h3 class="docSection1Title" id="ch05lev1sec1">5.1 What Is <span class="docEmphasis">awk?</span></h3>
<p class="docText"><span class="docEmphasis">Awk</span> is a programming
language used for manipulating data and generating reports. The data may come
from standard input, one or more files, or as output from a process.
<span class="docEmphasis">Awk</span> can be used at the command line for simple
operations, or it can be written into programs for larger applications. Because
<span class="docEmphasis">awk</span> can manipulate data, it is an indispensable
tool used in shell scripts and for managing small databases.</p>
<p class="docText"><span class="docEmphasis">Awk</span> scans a file (or input)
line by line, from the first to the last line, searching for lines that match a
specified pattern and performing selected actions (enclosed in curly braces) on
those lines. If there is a pattern with no specific action, all lines that match
the pattern are displayed; if there is an action with no pattern, all input
lines specified by the action are executed upon.</p>
<h4 class="docSection2Title" id="ch05lev2sec1">5.1.1 What Does <span class="docEmphasis">awk</span>
Stand for?</h4>
<p class="docText"><span class="docEmphasis">Awk</span> stands for the first
initials in the last names of each of the authors of the language, Alfred Aho,
Brian Kernighan, and Peter Weinberger. They could have called it
<span class="docEmphasis">wak</span> or <span class="docEmphasis">kaw,</span>
but for whatever reason, <span class="docEmphasis">awk</span> won out.</p>
<h4 class="docSection2Title" id="ch05lev2sec2">5.1.2 Which <span class="docEmphasis">awk?</span></h4>
<p class="docText">There are a number of versions of <span class="docEmphasis">
awk:</span> old <span class="docEmphasis">awk,</span> new
<span class="docEmphasis">awk,</span> gnu <span class="docEmphasis">awk (gawk),</span>
POSIX <span class="docEmphasis">awk,</span> and others.
<span class="docEmphasis">Awk</span> was originally written in 1977, and in
1985, the original implementation was improved so that <span class="docEmphasis">
awk</span> could handle larger programs. Additional features included
user-defined functions, dynamic regular expressions, processing multiple input
files, and more. On most systems, the command is <span class="docEmphasis">awk</span>
if using the old version, <span class="docEmphasis">nawk</span> if using the new
version, and <span class="docEmphasis">gawk</span> if using the gnu version.<span id="ENB5-1"><a class="docLink" href="#EN5-1"><sup>[1]</sup></a></span></p>
<h3 class="docSection1Title" id="ch05lev1sec2">5.2 <span class="docEmphasis">awk</span>'s Format</h3>
<p class="docText">An <span class="docEmphasis">awk</span> program consists of
the <span class="docEmphasis">awk</span> command, the program instructions
enclosed in quotes (or in a file), and the name of the input file. If an input
file is not specified, input comes from standard input (<span class="docEmphasis">stdin</span>),
the keyboard.</p>
<p class="docText"><span class="docEmphasis">Awk</span> instructions consist of
patterns, actions, or a combination of patterns and actions. A pattern is a
statement consisting of an expression of some type. If you do not see the
keyword <span class="docEmphasis">if,</span> but you <span class="docEmphasis">
think</span> the word <span class="docEmphasis">if</span> when evaluating the
expression, it is a pattern. Actions consist of one or more statements separated
by semicolons or newlines and enclosed in curly braces. Patterns cannot be
enclosed in curly braces, and consist of regular expressions enclosed in forward
slashes or expressions consisting of one or more of the many operators provided
by <span class="docEmphasis">awk.</span></p>
<p class="docText"><span class="docEmphasis">Awk</span> commands can be typed at
the command line or in <span class="docEmphasis">awk</span> script files. The
input lines can come from files, pipes, or standard input.</p>
<h4 class="docSection2Title" id="ch05lev2sec3">5.2.1 Input from Files</h4>
<p class="docText">In the following examples, the percent sign (%) is the C
shell prompt.</p>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">FORMAT</h2>
<pre>% nawk 'pattern' filename
% nawk '{action}' filename
% nawk 'pattern {action}' filename
</pre>
</td>
</tr>
</table>
<p class="docText">Here is a sample file called <span class="docEmphasis">
employees:</span></p>
<h5 id="ch05list01" class="docExampleTitle">Example 5.1 </h5>
<pre>% <span class="docEmphStrong">cat employees</span>
<span class="docEmphasis">Tom Jones 4424 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>
% <span class="docEmphStrong">nawk '/Mary/' employees</span>
<span class="docEmphasis">Mary Adams 5346 11/4/63 28765</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText"><span class="docEmphasis">Nawk</span> prints all lines
that contain the pattern <span class="docEmphasis">Mary.</span></td>
</tr>
</table>
<h5 id="ch05list02" class="docExampleTitle">Example 5.2 </h5>
<pre>% <span class="docEmphStrong">cat employees</span>
<span class="docEmphasis">Tom Jones 4424 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>
% <span class="docEmphStrong">nawk '{print $1}' employees</span>
<span class="docEmphasis">Tom</span>
<span class="docEmphasis">Mary</span>
<span class="docEmphasis">Sally</span>
<span class="docEmphasis">Billy</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText"><span class="docEmphasis">Nawk</span> prints the first
field of file <span class="docEmphasis">employees,</span> where the field
starts at the left margin of the line and is delimited by whitespace.</td>
</tr>
</table>
<h5 id="ch05list03" class="docExampleTitle">Example 5.3 </h5>
<pre>% <span class="docEmphStrong">cat employees</span>
<span class="docEmphasis">Tom Jones 4424 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>
% <span class="docEmphStrong">nawk '/Sally/{print $1, $2}' employees</span>
<span class="docEmphasis">Sally Chang</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText"><span class="docEmphasis">Nawk</span> prints the first
and second fields of file <span class="docEmphasis">employees,</span> only
if the line contains the pattern <span class="docEmphasis">Sally.</span>
Remember, the field separator is whitespace.</td>
</tr>
</table>
<h4 class="docSection2Title" id="ch05lev2sec4">5.2.2 Input from Commands</h4>
<p class="docText">The output from a UNIX command or commands can be piped to
<span class="docEmphasis">awk</span> for processing. Shell programs commonly use
<span class="docEmphasis">awk</span> for manipulating commands.</p>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">FORMAT</h2>
<pre>% command | nawk 'pattern'
% command | nawk '{action}'
% command | nawk 'pattern {action}'
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -