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

📄 _chapter 7.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The user-defined variables <span class="docEmphasis">
    month</span> and <span class="docEmphasis">year</span> are assigned the 
    values <span class="docEmphasis">4</span> and <span class="docEmphasis">
    2001,</span> respectively. In the <span class="docEmphasis">awk</span> 
    script, these variables may be used as though they were created in the 
    script. Note: If <span class="docEmphasis">filename</span> precedes the 
    arguments, the variables will not be available in the
    <span class="docEmphasis">BEGIN</span> statements. (See &quot;<a class="docLink" href="#ch07lev2sec3">BEGIN 
    Patterns</a>&quot;.)</td>
  </tr>
</table>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">杤</span> 
Option <span class="docEmphasis">(nawk)</span>.</span> The
<span class="docEmphasis">杤</span> option provided by <span class="docEmphasis">
nawk</span> allows command line arguments to be processed within a
<span class="docEmphasis">BEGIN</span> statement. For each argument passed at 
the command line, there must be a <span class="docEmphasis">杤</span> option 
preceding it.</p>
<p class="docText"><b>Field Variables.</b> Field variables can be used like 
user-defined variables, except they reference fields. New fields can be created 
by assignment. A field value that is referenced and has no value will be 
assigned the null string. If a field value is changed, the
<span class="docEmphasis">$0</span> variable is recomputed using the current 
value of <span class="docEmphasis">OFS</span> as a field separator. The number 
of fields allowed is usually limited to 100.</p>
<h5 id="ch07list03" class="docExampleTitle">Example 7.3 </h5>
<pre>% <span class="docEmphStrong">nawk ' { $5 = 1000 * $3 / $2;  print } '  filename</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">If <span class="docEmphasis">$5</span> does not exist,
    <span class="docEmphasis">awk</span> will create it and assign the result of 
    the expression <span class="docEmphasis">1000 * $3 / $2</span> to the fifth 
    field (<span class="docEmphasis">$5</span>). If the fifth field exists, the 
    result will be assigned to it, overwriting what is there.</td>
  </tr>
</table>
<h5 id="ch07list04" class="docExampleTitle">Example 7.4 </h5>
<pre>% <span class="docEmphStrong">nawk ' $4 == &quot;CA&quot; { $4  = &quot;California&quot;; print}'  filename</span></pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">If the fourth field (<span class="docEmphasis">$4</span>) 
    is equal to the string <span class="docEmphasis">CA,</span>
    <span class="docEmphasis">awk</span> will reassign the fourth field to
    <span class="docEmphasis">California.</span> The double quotes are 
    essential. Without them, the strings become user-defined variables with an 
    initial value of null.</td>
  </tr>
</table>
<p class="docText"><b>Built-In Variables.</b> Built-in variables have uppercase 
names. They can be used in expressions and can be reset. See
<a class="docLink" href="#ch07table02">Table 7.2</a> for a list of built-in 
variables.</p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
  <caption>
  <h5 id="ch07table02" class="docTableTitle">Table 7.2. Built-In Variables</h5>
  </caption>
  <colgroup span="2" align="left">
  </colgroup>
  <tr>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Variable Name</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Variable Contents</span> </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">ARGC</span>
    </td>
    <td class="docTableCell" vAlign="top">Number of command line argument. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">ARGV</span>
    </td>
    <td class="docTableCell" vAlign="top">Array of command line arguments. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">FILENAME</span>
    </td>
    <td class="docTableCell" vAlign="top">Name of current input file. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">FNR</span>
    </td>
    <td class="docTableCell" vAlign="top">Record number in current file. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">FS</span>
    </td>
    <td class="docTableCell" vAlign="top">The input field separator, by default 
    a space. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">NF</span>
    </td>
    <td class="docTableCell" vAlign="top">Number of fields in current record.
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">NR</span>
    </td>
    <td class="docTableCell" vAlign="top">Number of records so far. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">OFMT</span>
    </td>
    <td class="docTableCell" vAlign="top">Output format for numbers. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">OFS</span>
    </td>
    <td class="docTableCell" vAlign="top">Output field separator. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">ORS</span>
    </td>
    <td class="docTableCell" vAlign="top">Output record separator. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">RLENGTH</span>
    </td>
    <td class="docTableCell" vAlign="top">Length of string matched by
    <span class="docEmphasis">match</span> function. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">RS</span>
    </td>
    <td class="docTableCell" vAlign="top">Input record separator. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">RSTART</span>
    </td>
    <td class="docTableCell" vAlign="top">Offset of string matched by
    <span class="docEmphasis">match</span> function. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">SUBSEP</span>
    </td>
    <td class="docTableCell" vAlign="top">Subscript separator. </td>
  </tr>
</table>
<h5 id="ch07list05" class="docExampleTitle">Example 7.5 </h5>
<pre>(The Employees Database)
% <span class="docEmphStrong">cat employees2</span>
Tom Jones:4423:5/12/66:543354
Mary Adams:5346:11/4/63:28765
Sally Chang:1654:7/22/54:650000
Mary Black:1683:9/23/44:336500

(The Command Line)
% <span class="docEmphStrong">nawk  朏:  '$1 == &quot;Mary Adams&quot;{print NR, $1, $2, $NF}' employees2</span>

(The Output)
<span class="docEmphasis">2  Mary Adams 5346  28765</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">朏</span> option sets the 
    field separator to a colon. The <span class="docEmphasis">print</span> 
    function prints the record number, the first field, the second field, and 
    the last field (<span class="docEmphasis">$NF</span>).</td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch07lev2sec3">7.1.3 <span class="docEmphasis">BEGIN</span> 
Patterns</h4>
<p class="docText">The <span class="docEmphasis">BEGIN</span> pattern is 
followed by an action block that is executed <span class="docEmphasis">before</span>
<span class="docEmphasis">awk</span> processes any lines from the input file. In 
fact, a <span class="docEmphasis">BEGIN</span> block can be tested without any 
input file, since <span class="docEmphasis">awk</span> does not start reading 
input until the <span class="docEmphasis">BEGIN</span> action block has 
completed. The <span class="docEmphasis">BEGIN</span> action is often used to 
change the value of the built-in variables, <span class="docEmphasis">OFS, RS, 
FS,</span> and so forth, to assign initial values to user-defined variables and 
to print headers or titles as part of the output.</p>
<h5 id="ch07list06" class="docExampleTitle">Example 7.6 </h5>
<pre>% <span class="docEmphStrong">nawk 'BEGIN{FS=&quot;:&quot;; OFS=&quot;\t&quot;; ORS=&quot;\n\n&quot;}{print $1,$2,$3}' file</span></pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">Before the input file is processed, the field separator (<span class="docEmphasis">FS</span>) 
    is set to a colon, the output field separator (<span class="docEmphasis">OFS</span>) 
    to a tab, and the output record separator (<span class="docEmphasis">ORS</span>) 
    to two newlines. If there are two or more statements in the action block, 
    they should be separated with semicolons or placed on separate lines (use a 
    backslash to escape the newline character if at the shell prompt).</td>
  </tr>
</table>
<h5 id="ch07list07" class="docExampleTitle">Example 7.7 </h5>
<pre>% <span class="docEmphStrong">nawk 'BEGIN{print &quot;MAKE YEAR&quot;}'</span>
<span class="docEmphasis">make year</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 display
    <span class="docEmphasis">MAKE YEAR.</span> The <span class="docEmphasis">
    print</span> function is executed before <span class="docEmphasis">awk</span> 
    opens the input file, and even though the input file has not been assigned,
    <span class="docEmphasis">awk</span> will still print
    <span class="docEmphasis">MAKE</span> and <span class="docEmphasis">YEAR.</span> 
    When debugging <span class="docEmphasis">awk</span> scripts, you can test 
    the <span class="docEmphasis">BEGIN</span> block actions before writing the 
    rest of the program.</td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch07lev2sec4">7.1.4 <span class="docEmphasis">END</span> Patterns</h4>
<p class="docText"><span class="docEmphasis">END</span> patterns do not match 

⌨️ 快捷键说明

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