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

📄 _chapter 5.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 2 页
字号:
</pre>
    </td>
  </tr>
</table>
<h5 id="ch05list04" class="docExampleTitle">Example 5.4 </h5>
<pre>1   % <span class="docEmphStrong">df | nawk '$4 &gt; 75000'</span>
    <span class="docEmphasis">/oracle   (/dev/dsk/c0t0d057 ):390780 blocks      105756 files</span>
    <span class="docEmphasis">/opt      (/dev/dsk/c0t0d058 ):1943994 blocks      49187 files</span>

2   % <span class="docEmphStrong">rusers  | nawk '/root$/{print  $1}'</span>
    <span class="docEmphasis">owl</span>
    <span class="docEmphasis">crow</span>
    <span class="docEmphasis">bluebird</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">The <span class="docEmphasis">df</span> command reports 
      the free disk space on file systems. The output of the
      <span class="docEmphasis">df</span> command is piped to
      <span class="docEmphasis">nawk</span> (new <span class="docEmphasis">awk</span>). 
      If the fourth field is greater than 75,000 blocks, the line is printed.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">rusers</span> command 
      prints those logged on remote machines on the network. The output of the
      <span class="docEmphasis">rusers</span> command is piped to
      <span class="docEmphasis">nawk</span> as input. The first field is printed 
      if the regular expression <span class="docEmphasis">root</span> is matched 
      at the end of the line (<span class="docEmphasis">$</span>); that is, all 
      machine names are printed where <span class="docEmphasis">root</span> is 
      logged on.</span></li>
    </ol>
    </span></td>
  </tr>
</table>
<h3 class="docSection1Title" id="ch05lev1sec3">5.3 Formatting Output</h3>
<h4 class="docSection2Title" id="ch05lev2sec5">5.3.1 The <span class="docEmphasis">print</span> 
Function</h4>
<p class="docText">The action part of the <span class="docEmphasis">awk</span> 
command is enclosed in curly braces. If no action is specified and a pattern is 
matched, <span class="docEmphasis">awk</span> takes the default action, which is 
to print the lines that are matched to the screen. The <span class="docEmphasis">
print</span> function is used to print simple output that does not require fancy 
formatting. For more sophisticated formatting, the <span class="docEmphasis">
printf</span> or <span class="docEmphasis">sprintf</span> functions are used. If 
you are familiar with C, then you already know how <span class="docEmphasis">
printf</span> and <span class="docEmphasis">sprintf</span> work.</p>
<p class="docText">The <span class="docEmphasis">print</span> function can also 
be explicitly used in the action part of <span class="docEmphasis">awk</span> as
<span class="docEmphasis">{print}.</span> The <span class="docEmphasis">print</span> 
function accepts arguments as variables, computed values, or string constants. 
Strings must be enclosed in double quotes. Commas are used to separate the 
arguments; if commas are not provided, the arguments are concatenated together. 
The comma evaluates to the value of the output field separator (<span class="docEmphasis">OFS</span>), 
which is by default a space.</p>
<p class="docText">The output of the <span class="docEmphasis">print</span> 
function can be redirected or piped to another program, and the output of 
another program can be piped to <span class="docEmphasis">awk</span> for 
printing. (See &quot;Redirection&quot; on page 16 and &quot;Pipes&quot; on page 19.)</p>
<h5 id="ch05list05" class="docExampleTitle">Example 5.5 </h5>
<pre>% <span class="docEmphStrong">date</span>
<span class="docEmphasis">Wed Jul 28 22:23:16 PDT 2001</span>

% <span class="docEmphStrong">date | nawk '{ print &quot;Month: &quot; $2 &quot;\nYear: &quot; , $6 }'</span>
<span class="docEmphasis">Month: Jul</span>
<span class="docEmphasis">Year: 2001</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The output of the UNIX <span class="docEmphasis">date</span> 
    command will be piped to <span class="docEmphasis">nawk.</span> The string
    <span class="docEmphasis">Month:</span> is printed, followed by the second 
    field, the string containing the newline character,
    <span class="docEmphasis">\n,</span> and <span class="docEmphasis">Year:,</span> 
    followed by the sixth field (<span class="docEmphasis">$6</span>).</td>
  </tr>
</table>
<p class="docText"><b>Escape Sequences.</b> Escape sequences are represented by 
a backslash and a letter or number. They can be used in strings to represent 
tabs, newlines, form feeds, and so forth (see
<a class="docLink" href="#ch05table01">Table 5.1</a>).</p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
  <caption>
  <h5 id="ch05table01" class="docTableTitle">Table 5.1. <span class="docEmphasis">print</span> 
  Escape Sequences</h5>
  </caption>
  <colgroup span="2" align="left">
  </colgroup>
  <tr>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Escape Sequence</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Meaning</span> </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">\b</span>
    </td>
    <td class="docTableCell" vAlign="top">Backspace. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">\f</span>
    </td>
    <td class="docTableCell" vAlign="top">Form feed. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">\n</span>
    </td>
    <td class="docTableCell" vAlign="top">Newline. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">\r</span>
    </td>
    <td class="docTableCell" vAlign="top">Carriage return. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">\t</span>
    </td>
    <td class="docTableCell" vAlign="top">Tab. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">\047</span>
    </td>
    <td class="docTableCell" vAlign="top">Octal value 47, a single quote. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">\c</span>
    </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">c</span> 
    represents any other character, e.g., \&quot;. </td>
  </tr>
</table>
<h5 id="ch05list06" class="docExampleTitle">Example 5.6 </h5>
<pre><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 &quot;\t\tHave a nice day, &quot; $1, $2 &quot;\!&quot;}' employees</span>
        <span class="docEmphasis">Have a nice day, Sally Chang!</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">If the line contains the pattern
    <span class="docEmphasis">Sally,</span> the <span class="docEmphasis">print</span> 
    function prints two tabs, the string <span class="docEmphasis">Have a nice 
    day,</span> the first (where <span class="docEmphasis">$1</span> is
    <span class="docEmphasis">Sally</span>) and second fields (where
    <span class="docEmphasis">$2</span> is <span class="docEmphasis">Chang</span>), 
    followed by a string containing two exclamation marks.</td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch05lev2sec6">5.3.2 The <span class="docEmphasis">OFMT</span> 
Variable</h4>
<p class="docText">When printing numbers, you may want to control the format of 
the number. Normally this would be done with the <span class="docEmphasis">
printf</span> function, but the special <span class="docEmphasis">awk</span> 
variable, <span class="docEmphasis">OFMT,</span> can be set to control the 
printing of numbers when using the <span class="docEmphasis">print</span> 
function. It is set by default to &quot;<span class="docEmphasis">%.6g</span>&quot;<span class="docEmphasis">

⌨️ 快捷键说明

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