📄 _chapter 7.htm
字号:
any input lines, but execute any actions that are associated with the
<span class="docEmphasis">END</span> pattern. <span class="docEmphasis">END</span>
patterns are handled <span class="docEmphasis">after</span> all lines of input
have been processed.</p>
<h5 id="ch07list08" class="docExampleTitle">Example 7.8 </h5>
<pre>% <span class="docEmphStrong">nawk 'END{print "The number of records is " NR }' filename</span>
<span class="docEmphasis">The number of records is 4</span></pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">The <span class="docEmphasis">END</span> block is
executed after <span class="docEmphasis">awk</span> has finished processing
the file. The value of <span class="docEmphasis">NR</span> is the number of
the last record read.</td>
</tr>
</table>
<h5 id="ch07list09" class="docExampleTitle">Example 7.9 </h5>
<pre>% <span class="docEmphStrong">nawk '/Mary/{count++}END{print "Mary was found " count " times."}'\</span>
<span class="docEmphStrong">employees</span>
<span class="docEmphasis">Mary was found 2 times.</span></pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">For every line that contains the pattern
<span class="docEmphasis">sun,</span> the value of the
<span class="docEmphasis">count</span> variable is incremented by one. After
<span class="docEmphasis">awk</span> has processed the entire file, the
<span class="docEmphasis">END</span> block prints the string
<span class="docEmphasis">Sun was found,</span> the value of
<span class="docEmphasis">count,</span> and the string
<span class="docEmphasis">times.</span></td>
</tr>
</table>
<h3 class="docSection1Title" id="ch07lev1sec2">7.2 Redirection and Pipes</h3>
<h4 class="docSection2Title" id="ch07lev2sec5">7.2.1 Output Redirection</h4>
<p class="docText">When redirecting output from within <span class="docEmphasis">
awk</span> to a UNIX file, the shell redirection operators are used. The
filename must be enclosed in double quotes. When the > symbol is used, the file
is opened and truncated. Once the file is opened, it remains open until
explicitly closed or the <span class="docEmphasis">awk</span> program
terminates. Output from subsequent <span class="docEmphasis">print</span>
statements to that file will be appended to the file.</p>
<p class="docText">The <span class="docEmphasis">>></span> symbol is used to
open the file, but does not clear it out; instead it simply appends to it.</p>
<h5 id="ch07list10" class="docExampleTitle">Example 7.10 </h5>
<pre>% <span class="docEmphStrong">nawk '$4 >= 70 {print $1, $2 > "passing_file" }' filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">If the value of the fourth field is greater than or equal
to <span class="docEmphasis">70,</span> the first and second fields will be
printed to the file <span class="docEmphasis">passing_ file.</span></td>
</tr>
</table>
<h4 class="docSection2Title" id="ch07lev2sec6">7.2.2 Input Redirection <span class="docEmphasis">(getline)</span></h4>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
getline</span> Function.</span> The <span class="docEmphasis">getline</span>
function is used to read input from the standard input, a pipe, or a file other
than the current file being processed. It gets the next line of input and sets
the <span class="docEmphasis">NF, NR,</span> and the <span class="docEmphasis">
FNR</span> built-in variables. The <span class="docEmphasis">getline</span>
function returns one if a record is found and zero if EOF (end of file) is
reached. If there is an error, such as failure to open a file, the
<span class="docEmphasis">getline</span> function returns a value of
<span class="docEmphasis">-1.</span></p>
<h5 id="ch07list11" class="docExampleTitle">Example 7.11 </h5>
<pre>% <span class="docEmphStrong">nawk 'BEGIN{ "date" | getline d; print d}' filename</span>
<span class="docEmphasis">Thu Jan 14 11:24:24 PST 2001</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">Will execute the UNIX <span class="docEmphasis">date</span>
command, pipe the output to <span class="docEmphasis">getline,</span> assign
it to the user-defined variable <span class="docEmphasis">d,</span> and then
print <span class="docEmphasis">d.</span></td>
</tr>
</table>
<h5 id="ch07list12" class="docExampleTitle">Example 7.12 </h5>
<pre>% <span class="docEmphStrong">nawk 'BEGIN{ "date " | getline d; split( d, mon) ; print mon[2]}'\</span>
<span class="docEmphStrong">filename</span>
<span class="docEmphasis">Jan</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">Will execute the <span class="docEmphasis">date</span>
command and pipe the output to <span class="docEmphasis">getline.</span> The
<span class="docEmphasis">getline</span> function will read from the pipe
and store the input in a user-defined variable, <span class="docEmphasis">d.</span>
The <span class="docEmphasis">split</span> function will create an array
called <span class="docEmphasis">mon</span> out of variable
<span class="docEmphasis">d</span> and then the second element of the array
<span class="docEmphasis">mon</span> will be printed.</td>
</tr>
</table>
<h5 id="ch07list13" class="docExampleTitle">Example 7.13 </h5>
<pre><span class="docEmphasis">%</span> <span class="docEmphStrong">nawk 'BEGIN{while("ls" | getline) print}'</span>
<span class="docEmphasis">a.out</span>
<span class="docEmphasis">db</span>
<span class="docEmphasis">dbook</span>
<span class="docEmphasis">getdir</span>
<span class="docEmphasis">file</span>
<span class="docEmphasis">sortedf</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText">Will send the output of the <span class="docEmphasis">ls</span>
command to <span class="docEmphasis">getline;</span> for each iteration of
the loop, <span class="docEmphasis">getline</span> will read one more line
of the output from <span class="docEmphasis">ls</span> and then print it to
the screen. An input file is not necessary, since the
<span class="docEmphasis">BEGIN</span> block is processed before
<span class="docEmphasis">awk</span> attempts to open input.</td>
</tr>
</table>
<h5 id="ch07list14" class="docExampleTitle">Example 7.14 </h5>
<pre>(The Command Line)
1 % <span class="docEmphStrong">nawk 'BEGIN{ printf "What is your name?" ;\</span>
<span class="docEmphStrong">getline name < "/dev/tty"}\</span>
2 <span class="docEmphStrong">$1 ~ name {print "Found " name " on line ", NR "."}\</span>
3 <span class="docEmphStrong">END{print "See ya, " name "."}' filename</span>
(The Output)
<span class="docEmphasis">What is your name?</span> <span class="docEmphBoldItalic">Ellie</span> <span class="docEmphasis">< Waits for input from user ></span>
<span class="docEmphasis">Found Ellie on line 5.</span>
<span class="docEmphasis">See ya, Ellie.</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">Will print to the screen <span class="docEmphasis">What
is your name?</span> and wait for user response; the
<span class="docEmphasis">getline</span> function will accept input from
the terminal (<span class="docEmphasis">/dev/tty</span>) until a newline
is entered, and then store the input in the user-defined variable
<span class="docEmphasis">name.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">If the first field matches the value assigned to
<span class="docEmphasis">name,</span> the <span class="docEmphasis">print</span>
function is executed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">END</span> statement
prints out <span class="docEmphasis">See ya,</span> and then the value
<span class="docEmphasis">Ellie,</span> stored in variable
<span class="docEmphasis">name,</span> is displayed, followed by a period<span class="docEmphasis">.</span></span></li>
</ol>
</span></td>
</tr>
</table>
<h5 id="ch07list15" class="docExampleTitle">Example 7.15 </h5>
<pre>(The Command Line)
% <span class="docEmphStrong">nawk 'BEGIN{while (getline < "/etc/passwd" > 0 )lc++; print lc}'\</span>
<span class="docEmphStrong">file</span>
(The Output)
<span class="docEmphasis">16</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 read each line
from the <span class="docEmphasis">/etc/passwd</span> file, increment
<span class="docEmphasis">lc</span> until EOF is reached, then print the
value of <span class="docEmphasis">lc,</span> which is the number of lines
in the <span class="docEmphasis">passwd</span> file.</p>
<div class="docNote">
<p class="docNoteTitle">Note</p>
<p class="docText">The value returned by <span class="docEmphasis">getline</span>
is negative one if the file does not exist. If the end of file is reached,
the return value is zero, and if a line was read, the return value is one.
Therefore, the command</p>
<pre>while ( getline < "/etc/junk")
</pre>
<p class="docText">would start an infinite loop if the file
<span class="docEmphasis">/etc/junk</span> did not exist, since the return
value of negative one yields a true condition.</div>
</td>
</tr>
</table>
<h3 class="docSection1Title" id="ch07lev1sec3">7.3 Pipes</h3>
<p class="docText">If you open a pipe in an <span class="docEmphasis">awk</span>
program, you must close it before opening another one. The command on the
right-hand side of the pipe symbol is enclosed in double quotes. Only one pipe
can be open at a time.</p>
<h5 id="ch07list16" class="docExampleTitle">Example 7.16 </h5>
<pre>(The Database)
% <span class="docEmphStrong">cat names</span>
<span class="docEmphasis">john smith</span>
<span class="docEmphasis">alice cheba</span>
<span class="docEmphasis">george goldberg</span>
<span class="docEmphasis">susan goldberg</span>
<span class="docEmphasis">tony tram</span>
<span class="docEmphasis">barbara nguyen</span>
<span class="docEmphasis">elizabeth lone</span>
<span class="docEmphasis">dan savage</span>
<span class="docEmphasis">eliza goldberg</span>
<span class="docEmphasis">john goldenrod</span>
(The Command Line)
% <span class="docEmphStrong">nawk '{print $1, $2 | "sort 杛 +1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -