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

📄 _chapter 3.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 4 页
字号:
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">[^]</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches one character not in the set
    </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'[^A朘]ove'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches lines not containing a 
    character in the range <span class="docEmphasis">A</span> through
    <span class="docEmphasis">K,</span> followed by <span class="docEmphasis">
    ove.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">\&lt; </td>
    <td class="docTableCell" vAlign="top">Beginning-of-word anchor </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'\&lt;love'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches lines containing a word that 
    begins with <span class="docEmphasis">love.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">\&gt; </td>
    <td class="docTableCell" vAlign="top">End-of-word anchor </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'love\&gt;'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches lines containing a word that 
    ends with <span class="docEmphasis">love.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">\(..\) </td>
    <td class="docTableCell" vAlign="top">Tags matched characters </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'\(love\)ing'</span>
    </td>
    <td class="docTableCell" vAlign="top">Tags marked portion in a register to 
    be remembered later as number 1. To reference later, use \<span class="docEmphasis">1</span> 
    to repeat the pattern. May use up to nine tags, starting with the first tag 
    at the left-most part of the pattern. For example, the pattern
    <span class="docEmphasis">love</span> is saved in register 1 to be 
    referenced later as \<span class="docEmphasis">1.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">x\{m\} 
    x\{m,\} x\{m,n\}</span><sup class="docFootnote"><a class="docLink" href="#ch03tabfn01">[a]</a></sup>
    </td>
    <td class="docTableCell" vAlign="top">Repetition of character x: m times, at 
    least m times, or between m and n times </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'o\{5\}' 
    'o\{5,\}' 'o\{5,10\}'</span> </td>
    <td class="docTableCell" vAlign="top">Matches if line has 5 occurences of
    <span class="docEmphasis">o</span>, at least 5 occurences of
    <span class="docEmphasis">o</span>, or between 5 and 10 occurrences of
    <span class="docEmphasis">o</span>. </td>
  </tr>
</table>
<blockquote>
  <p class="docFootnote"><sup><a name="ch03tabfn01">[a]</a></sup> The \{ \} 
  metacharacters are not supported on all versions of UNIX or all 
  pattern-matching utilities; they usually work with <span class="docEmphasis">
  vi</span> and <span class="docEmphasis">grep.</span></p>
</blockquote>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
  <caption>
  <h5 id="ch03table02" class="docTableTitle">Table 3.2. <span class="docEmphasis">grep</span>'s 
  Options</h5>
  </caption>
  <colgroup span="2" align="left">
  </colgroup>
  <tr>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Option</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">What 
    It Does</span> </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">朾</span>
    </td>
    <td class="docTableCell" vAlign="top">Precedes each line by the block number 
    on which it was found. This is sometimes useful in locating disk block 
    numbers by context. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">朿</span>
    </td>
    <td class="docTableCell" vAlign="top">Displays a count of matching lines 
    rather than displaying the lines that match. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">杊</span>
    </td>
    <td class="docTableCell" vAlign="top">Does not display filenames. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">杋</span>
    </td>
    <td class="docTableCell" vAlign="top">Ignores the case of letters in making 
    comparisons (i.e., upper-- and lowercase are considered identical). </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">杔</span>
    </td>
    <td class="docTableCell" vAlign="top">Lists only the names of files with 
    matching lines (once), separated by newline characters. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">杗</span>
    </td>
    <td class="docTableCell" vAlign="top">Precedes each line by its relative 
    line number in the file. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">杝</span>
    </td>
    <td class="docTableCell" vAlign="top">Works silently, that is, displays 
    nothing except error messages. This is useful for checking the exit status.
    </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">杤</span>
    </td>
    <td class="docTableCell" vAlign="top">Inverts the search to display only 
    lines that do not match. </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">杦</span>
    </td>
    <td class="docTableCell" vAlign="top">Searches for the expression as a word, 
    as if surrounded by <span class="docEmphasis">\&lt;</span> and
    <span class="docEmphasis">\&gt;.</span> This applies to
    <span class="docEmphasis">grep</span> only. (Not all versions of
    <span class="docEmphasis">grep</span> support this feature; e.g., SCO UNIX 
    does not.) </td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch03lev2sec3">3.1.3 <span class="docEmphasis">grep</span> and 
Exit Status</h4>
<p class="docText">The <span class="docEmphasis">grep</span> command is very 
useful in shell scripts, because it always returns an exit status to indicate 
whether it was able to locate the pattern or the file you were looking for. If 
the pattern is found, <span class="docEmphasis">grep</span> returns an exit 
status of 0, indicating success; if <span class="docEmphasis">grep</span> cannot 
find the pattern, it returns 1 as its exit status; and if the file cannot be 
found, <span class="docEmphasis">grep</span> returns an exit status of 2. (Other 
UNIX utilities that search for patterns, such as <span class="docEmphasis">sed</span> 
and <span class="docEmphasis">awk,</span> do not use the exit status to indicate 
the success or failure of locating a pattern; they report failure only if there 
is a syntax error in a command.)</p>
<p class="docText">In the following example, <span class="docEmphasis">john</span> 
is not found in the <span class="docEmphasis">/etc/passwd</span> file.</p>
<h5 id="ch03list04" class="docExampleTitle">Example 3.4 </h5>
<pre>1   % <span class="docEmphStrong">grep 'john' /etc/passwd</span>
2   % <span class="docEmphStrong">echo $status</span> <span class="docEmphasis">(csh)</span>
    <span class="docEmphasis">1</span>

    or

    $ <span class="docEmphStrong">echo $?</span>  <span class="docEmphasis">(sh, ksh)</span>
    <span class="docEmphasis">1</span></pre>
<div align="center">
  <center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
  <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"><span class="docEmphasis">Grep</span> searches for
      <span class="docEmphasis">john</span> in the <span class="docEmphasis">
      /etc/passwd</span> file, and if successful, <span class="docEmphasis">grep</span> 
      exits with a status of 0. If <span class="docEmphasis">john</span> is not 
      found in the file, <span class="docEmphasis">grep</span> exits with 1. If 
      the file is not found, an exit status of 2 is returned.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The C shell variable, <span class="docEmphasis">status,</span> 
      and the Bourne/Korn shell variable, ?, are assigned the exit status of the 
      last command that was executed.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

  </center>
</div>

<h3 class="docSection1Title" id="ch03lev1sec2">3.2 <span class="docEmphasis">grep</span> Examples 
with Regular Expressions</h3>
<p class="docText">The file being used for these examples is called
<span class="docEmphasis">datafile.</span></p>
<pre>% <span class="docEmphStrong">cat datafile</span>
<span class="docEmphasis">northwest           NW   Charles Main       3.0    .98    3     34</span>
<span class="docEmphasis">western             WE   Sharon Gray        5.3    .97    5     23</span>
<span class="docEmphasis">southwest           SW   Lewis Dalsass      2.7    .8     2     18</span>
<span class="docEmphasis">southern            SO   Suan Chin          5.1    .95    4     15</span>
<span class="docEmphasis">southeast           SE   Patricia Hemenway  4.0    .7     4     17</span>
<span class="docEmphasis">eastern             EA   TB Savage          4.4    .84    5     20</span>
<span class="docEmphasis">northeast           NE   AM Main Jr.        5.1    .94    3     13</span>
<span class="docEmphasis">north               NO   Margot Weber       4.5    .89    5      9</span>
<span class="docEmphasis">central             CT   Ann Stephens       5.7    .94    5     13</span>
</pre>
<h5 id="ch03list05" class="docExampleTitle">Example 3.5 </h5>
<pre><span class="docEmphStrong">grep NW datafile</span>
<span class="docEmphasis">northwest    NW    Charles Main    3.0   .98    3    34</span>
</pre>
<div align="center">
  <center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">Prints all lines containing the regular expression
    <span class="docEmphasis">NW</span> in a file called
    <span class="docEmphasis">datafile.</span></td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list06" class="docExampleTitle">Example 3.6 </h5>
<pre><span class="docEmphStrong">grep NW d*</span>
<span class="docEmphasis">datafile: northwest  NW   Charles Main   3.0  .98  3    34</span>
<span class="docEmphasis">db:northwest         NW   Joel Craig     30   40   5    123</span>
</pre>
<div align="center">
  <center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">Prints all lines containing the regular expression
    <span class="docEmphasis">NW</span> in all files starting with a
    <span class="docEmphasis">d.</span> The shell expands
    <span class="docEmphasis">d*</span> to all files that begin with a
    <span class="docEmphasis">d,</span> in this case the filenames are
    <span class="docEmphasis">db</span> and <span class="docEmphasis">datafile.</span></td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list07" class="docExampleTitle">Example 3.7 </h5>
<pre><span class="docEmphStrong">grep '^n' datafile</span>
<span class="docEmphasis">northwest        NW     Charles Main     3.0  .98   3   34</span>

⌨️ 快捷键说明

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