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

📄 _chapter 4.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 3 页
字号:
    </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">
    <p class="docText"><span class="docEmphasis">x\{m\}</span></p>
    <p class="docText"><span class="docEmphasis">x\{m,\}</span></p>
    <p class="docText"><span class="docEmphasis">x\{m,n\}</span><sup class="docFootnote"><a class="docLink" href="#ch04tabfn01">[a]</a></sup></td>
    <td class="docTableCell" vAlign="top">
    <p class="docText">Repetition of character x: m times</p>
    <p class="docText">at least m times</p>
    <p class="docText">at least m and not more than n times</td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">/o\{5\}/</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches if line has 5 occurrences of
    <span class="docEmphasis">o,</span> at least 5 occurrences 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="ch04tabfn01">[a]</a></sup> Not dependable
  on all versions of UNIX or all pattern-matching utilities; usually works with
  <span class="docEmphasis">vi</span> and <span class="docEmphasis">grep.</span></p>
</blockquote>
<h3 class="docSection1Title" id="ch04lev1sec6">4.6 <span class="docEmphasis">sed</span> Examples</h3>
<pre>% <span class="docEmphStrong">cat datafile</span>
<span class="docEmphasis">northwest          NW   Charles Main       3.0    .98    3    43</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>
<h4 class="docSection2Title" id="ch04lev2sec2">4.6.1 Printing: The <span class="docEmphasis">p</span>
Command</h4>
<h5 id="ch04list04" class="docExampleTitle">Example 4.4 </h5>
<pre><span class="docEmphStrong">sed '/north/p' datafile</span>
<span class="docEmphasis">northwest     NW     Charles Main        3.0  .98   3   34</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">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">north         NO     Margot Weber        4.5  .89   5   9</span>
<span class="docEmphasis">central       CT     Ann Stephens        5.7  .94   5   13</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">Prints all lines to standard output by default. If the
    pattern <span class="docEmphasis">north</span> is found,
    <span class="docEmphasis">sed</span> will print that line in addition to all
    the other lines.</td>
  </tr>
</table>
<h5 id="ch04list05" class="docExampleTitle">Example 4.5 </h5>
<pre><span class="docEmphStrong">sed -n '/north/p' 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>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The <span class="docEmphasis">-n</span> option suppresses
    the default behavior of <span class="docEmphasis">sed</span> when used with
    the <span class="docEmphasis">p</span> command. Without the
    <span class="docEmphasis">-n</span> option, <span class="docEmphasis">sed</span>
    will print duplicate lines of output as shown in the preceding example. Only
    the lines containing the pattern <span class="docEmphasis">north</span> are
    printed when <span class="docEmphasis">-n</span> is used.</td>
  </tr>
</table>
<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>
<h4 class="docSection2Title" id="ch04lev2sec3">4.6.2 Deleting: The <span class="docEmphasis">d</span>
Command</h4>
<h5 id="ch04list06" class="docExampleTitle">Example 4.6 </h5>
<pre><span class="docEmphStrong">sed '3d' 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">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>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">Deletes the third line. All other lines are printed to
    the screen by default.</td>
  </tr>
</table>
<h5 id="ch04list07" class="docExampleTitle">Example 4.7 </h5>
<pre><span class="docEmphStrong">sed '3,$d' 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>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The third line through the last line are deleted. The
    remaining lines are printed. The dollar sign (<span class="docEmphasis">$</span>)
    represents the last line of the file. The comma is called the
    <span class="docEmphasis">range operato</span>r. In this example, the range
    of addresses starts at line 3 and ends at the last line, which is
    represented by the dollar sign (<span class="docEmphasis">$</span>).</td>
  </tr>
</table>
<h5 id="ch04list08" class="docExampleTitle">Example 4.8 </h5>
<pre><span class="docEmphStrong">sed '$d' 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>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">Deletes the last line. The dollar sign (<span class="docEmphasis">$</span>)
    represents the last line. The default is to print all of the lines except
    those affected by the <span class="docEmphasis">d</span> command.</td>
  </tr>
</table>
<h5 id="ch04list09" class="docExampleTitle">Example 4.9 </h5>
<pre><span class="docEmphStrong">sed '/north/d' datafile</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">central       CT     Ann Stevens         5.7  .94   5   13</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">All lines containing the pattern
    <span class="docEmphasis">north</span> are deleted. The remaining lines are
    printed.</td>
  </tr>
</table>
<h4 class="docSection2Title" id="ch04lev2sec4">4.6.3 Substitution: The <span class="docEmphasis">s</span>
Command</h4>
<h5 id="ch04list10" class="docExampleTitle">Example 4.10 </h5>
<pre><span class="docEmphStrong">sed 's/west/north/g' datafile</span>
<span class="docEmphasis">northnorth    NW     Charles Main        3.0  .98   3   34</span>
<span class="docEmphasis">northern      WE     Sharon Gray         5.3  .97   5   23</span>
<span class="docEmphasis">southnorth    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>
<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The <span class="docEmphasis">s</span> command is for
    substitution. The <span class="docEmphasis">g</span> flag at the end of the
    command indicates that the substitution is global across the line; that is,
    if multiple occurrences of <span class="docEmphasis">west</span> are found,
    all of them will be replaced with <span class="docEmphasis">north.</span>
    Without the <span class="docEmphasis">g</span> command, only the first
    occurrence of <span class="docEmphasis">west</span> on each line would be
    replaced with <span class="docEmphasis">north.</span></td>
  </tr>
</table>
<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="ch04list11" class="docExampleTitle">Example 4.11 </h5>
<pre><span class="docEmphStrong">sed -n 's/^west/north/p' datafile</span>
<span class="docEmphasis">northern      WE     Sharon Gray        5.3  .97   5   23</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">s</span> command is for
    substitution. The <span class="docEmphasis">-n</span> option with the
    <span class="docEmphasis">p</span> flag at the end of the command tells
    <span class="docEmphasis">sed</span> to print only those lines where the
    substitution occurred; that is, if <span class="docEmphasis">west</span> is
    found at the beginning of the line and is replaced with
    <span class="docEmphasis">north,</span> just those lines are printed.</td>
  </tr>
</table>
<h5 id="ch04list12" class="docExampleTitle">Example 4.12 </h5>
<pre><span class="docEmphStrong">sed 's/[0

⌨️ 快捷键说明

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