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

📄 _chapter 3.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<pre><span class="docEmphStrong">grep '\(3\)\.[0-9].*\1     *\1' 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 the line if it contains a
    <span class="docEmphasis">3</span> followed by a period and another number, 
    followed by any number of characters (<span class="docEmphasis">.*</span> ), 
    another <span class="docEmphasis">3</span> (originally tagged), any number 
    of tabs, and another <span class="docEmphasis">3.</span> Since the
    <span class="docEmphasis">3</span> was enclosed in parentheses,
    <span class="docEmphasis">\(3\),</span> it can be later referenced with
    <span class="docEmphasis">\1.</span> <span class="docEmphasis">\1</span> 
    means that this was the first expression to be tagged with the
    <span class="docEmphasis">\( \)</span> pair.</td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list19" class="docExampleTitle">Example 3.19 </h5>
<pre><span class="docEmphStrong">grep '\&lt;north' datafile</span>
<span class="docEmphasis">northwest        NW     Charles Main     3.0  .98   3   34</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>
</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 a word starting with
    <span class="docEmphasis">north.</span> The <span class="docEmphasis">\&lt;</span> 
    is the beginning-of-word anchor.</td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list20" class="docExampleTitle">Example 3.20 </h5>
<pre><span class="docEmphStrong">grep '\&lt;north\&gt;' datafile</span>
<span class="docEmphasis">north            NO     Margot Weber     4.5  .89   5   9</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 the line if it contains the word
      <span class="docEmphasis">north.</span> The <span class="docEmphasis">\&lt;</span> 
      is the beginning-of-word anchor, and the <span class="docEmphasis">\&gt;</span> 
      is the end-of-word anchor.</td>
    </tr>
  </table>
  </center>
</div>
<h5 id="ch03list21" class="docExampleTitle">Example 3.21 </h5>
<pre><span class="docEmphStrong">grep '\&lt;[a-z].*n\&gt;' 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">southern         SO     Suan Chin       5.1  .95   4   15</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">central          CT     Ann Stephens    5.7  .94   5   13</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 a word starting with a 
      lowercase letter, followed by any number of characters, and a word ending 
      in <span class="docEmphasis">n.</span> Watch the
      <span class="docEmphBoldItalic">.*</span> symbol. It means any character, 
      including whitespace.</td>
    </tr>
  </table>
  </center>
</div>

<h3 class="docSection1Title" id="ch03lev1sec3">3.3 <span class="docEmphasis">grep</span> with 
Pipes</h3>
<p class="docText">Instead of taking its input from a file,
<span class="docEmphasis">grep</span> often gets its input from a pipe.</p>
<h5 id="ch03list22" class="docExampleTitle">Example 3.22 </h5>
<pre>% <span class="docEmphStrong">ls -l</span>
<span class="docEmphasis">drwxrwxrwx  2  ellie    2441 Jan 6 12:34    dir1</span>
<span class="docEmphasis">-rw-r--r--  1  ellie    1538 Jan 2 15:50    file1</span>
<span class="docEmphasis">-rw-r--r--  1  ellie    1539 Jan 3 13:36    file2</span>
<span class="docEmphasis">drwxrwxrwx  2  ellie    2341 Jan 6 12:34    grades</span>

% <span class="docEmphStrong">ls -l | grep '^d'</span>
<span class="docEmphasis">drwxrwxrwx  2  ellie    2441 Jan 6 12:34    dir1</span>
<span class="docEmphasis">drwxrwxrwx  2  ellie    2341 Jan 6 12:34    grades</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 <span class="docEmphasis">ls</span> 
    command is piped to <span class="docEmphasis">grep.</span> All lines of 
    output that begin with a <span class="docEmphasis">d</span> are printed; 
    that is, all directories are printed.</td>
  </tr>
</table>

<h3 class="docSection1Title" id="ch03lev1sec4">3.4 <span class="docEmphasis">grep</span> with 
Options</h3>
<p class="docText">The <span class="docEmphasis">grep</span> command has a 
number of options that control its behavior. Not all versions of UNIX support 
exactly the same options, so be sure to check your man pages for a complete 
list.</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       53     .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="ch03list23" class="docExampleTitle">Example 3.23 </h5>
<pre><span class="docEmphStrong">grep 杗 '^south' datafile</span>
<span class="docEmphasis">3:southwest      SW     Lewis Dalsass       2.7   .8    2   18</span>
<span class="docEmphasis">4:southern       SO     Suan Chin           5.1   .95   4   15</span>
<span class="docEmphasis">5:southeast      SE     Patricia Hemenway   4.0   .7    4   17</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">The <span class="docEmphasis">杗</span> option precedes 
    each line with the number of the line where the pattern was found, followed 
    by the line.</td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list24" class="docExampleTitle">Example 3.24 </h5>
<pre><span class="docEmphStrong">grep 杋 'pat' datafile</span>
<span class="docEmphasis">southeast        SE     Patricia Hemenway    4.0   .7    4    17</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">The <span class="docEmphasis">杋</span> option turns off 
    case sensitivity. It does not matter if the expression
    <span class="docEmphasis">pat</span> contains any combination of upper- or 
    lowercase letters.</td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list25" class="docExampleTitle">Example 3.25 </h5>
<pre><span class="docEmphStrong">grep 杤 'Suan Chin' 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">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>
<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">Here, the <span class="docEmphasis">杤</span> option 
    prints all lines <span class="docEmphasis">not</span> containing the pattern
    <span class="docEmphasis">Suan Chin.</span> This option is used when 
    deleting a specific entry from the input file. To really remove the entry, 
    you would redirect the output of <span class="docEmphasis">grep</span> to a 
    temporary file, and then change the name of the temporary file back to the 
    name of the original file as shown here:</p>
    <pre>grep -v 'Suan Chin' datafile &gt; temp
mv temp datafile
</pre>
    <p class="docText">Remember that you must use a temporary file when 
    redirecting the output from <span class="docEmphasis">datafile.</span> If 
    you redirect from <span class="docEmphasis">datafile</span> to
    <span class="docEmphasis">datafile,</span> the shell will &quot;clobber&quot; the
    <span class="docEmphasis">datafile.</span> (See &quot;Redirection&quot; on page 16.)</td>
  </tr>
</table>
  </center>
</div>
<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        53     .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="ch03list26" class="docExampleTitle">Example 3.26 </h5>
<pre><span class="docEmphStrong">grep 杔 'SE'  *</span>
<span class="docEmphasis">datafile</span>
<span class="docEmphasis">datebook</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">The <span class="docEmphasis">杔</span> option causes
    <span class="docEmphasis">grep</span> to print out only the filenames where 
    the pattern is found instead of the line of text</td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list27" class="docExampleTitle">Example 3.27 </h5>
<pre><span class="docEmphStrong">grep 朿 'west' datafile</span>
<span class="docEmphasis">3</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">The 

⌨️ 快捷键说明

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