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

📄 ch10_05.htm

📁 unix基础教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
lines.)  Using <tt class="literal">\n</tt> to match the embedded newline, you can matchpatterns across multiple lines.  See Example under <b class="emphasis-bold"><a href="#D">D</a></b>.</p><h4 class="refsect2">Examples</h4><p>Like the Example in <b class="emphasis-bold">n</b>, but print <tt class="literal">.NH</tt> line as well as header title:</p><blockquote><pre class="code">/^\.NH/{Np}</pre></blockquote><p>Join two lines (replace newline with space):</p><blockquote><pre class="code">/^\.NH/{Ns/\n/ /p}</pre></blockquote></td></tr><tr><td valign="top">p</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">p</tt></p><p><a name="IXT-10-123251" />Print the addressed line(s).  Note that this can result in duplicateoutput unless default output is suppressed using <tt class="literal">#n</tt> orthe <tt class="literal">-n</tt>command-line option.  Typically used before commands that change flowcontrol (<tt class="literal">d</tt>, <tt class="literal">n</tt>,<tt class="literal">b</tt>) and might prevent the current line from beingoutput.See Examples under <b class="emphasis-bold"><a href="#h">h</a></b>,<b class="emphasis-bold"><a href="#n">n</a></b>, and <b class="emphasis-bold"><a href="#N">N</a></b>.</p></td></tr><tr><td valign="top">P</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">P</tt></p><p><a name="IXT-10-123252" />Print first part (up to embedded newline) of multiline pattern space createdby <tt class="literal">N</tt> command.  Same as <tt class="literal">p</tt> if <tt class="literal">N</tt> has not been appliedto a line.</p><h4 class="refsect2">Example</h4><p>Suppose you have function references in two formats:</p><blockquote><pre class="code">function(arg1, arg2)function(arg1,         arg2)</pre></blockquote><p>The following script changes argument <tt class="literal">arg2</tt>, regardless of whether it appears on the same line as the function name:</p><blockquote><pre class="code">s/function(arg1, arg2)/function(arg1, XX)//function(/{Ns/arg2/XX/PD}</pre></blockquote></td></tr><tr><td valign="top">q</td><td><p>[<em class="replaceable"><tt>address</tt></em>]<tt class="literal">q</tt></p><p><a name="IXT-10-123253" />Quit when <em class="emphasis">address</em> is encountered.The addressed line is firstwritten to the output (if default output is not suppressed),along with any text appended to it by previous <tt class="literal">a</tt> or <tt class="literal">r</tt> commands.</p><h4 class="refsect2">Examples</h4><p>Delete everything after the addressed line:</p><blockquote><pre class="code">/Garbled text follows:/q</pre></blockquote><p>Print only the first 50 lines of a file:</p><blockquote><pre class="code">50q</pre></blockquote></td></tr><tr><td valign="top">r</td><td><p>[<em class="replaceable"><tt>address</em>]<tt class="literal">r</tt> <em class="replaceable">file</tt></em></p><p><a name="IXT-10-123254" />Read contents of <em class="emphasis">file</em> and append after the contents of thepattern space.There must beexactly one space between the <tt class="literal">r</tt> and the filename.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code">/The list of items follows:/r item_file</pre></blockquote></td></tr><tr><td valign="top">s</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</em>]]<tt class="literal">s/</tt><em class="replaceable">pattern</em><tt class="literal">/</tt><em class="replaceable">replacement</em><tt class="literal">/</tt>[<em class="replaceable">flags</tt></em>]</p><p><a name="IXT-10-123255" />Substitute <em class="emphasis">replacement</em> for <em class="emphasis">pattern</em> on each addressed line.If pattern addresses are used, the pattern <tt class="literal">//</tt> represents the lastpattern address specified.Any delimiter may be used. Use <tt class="literal">\</tt> within<em class="emphasis">pattern</em> or <em class="emphasis">replacement</em>to escape the delimiter.The following flags can be specified:</p><dl><dt><tt class="literal">g</tt></dt><dd>Replace all instances of <em class="emphasis">pattern</em></tt> on each addressed line, notjust the first instance.</p></dd><dt><tt class="literal">p</tt></dt><dd>Print the line if a successful substitution is done.  If severalsuccessful substitutions are done,<tt class="literal">sed</tt> prints multiple copies of the line.</p></dd><dt><tt class="literal">w</tt> <tt><em class="replaceable">file</em></tt></dt><dd>Write the line to <em class="emphasis">file</em></tt> if a replacement was done.  A maximumof 10 different <em class="emphasis">files</em></tt> can be opened.</p></dd><dt><i><em class="emphasis">n</em></tt></i></dt><dd>Replace <em class="emphasis">n</em></tt>th instance of<em class="emphasis">pattern</em> on each addressed line.<em class="emphasis">n</em> is any number in the range 1 to 512, and the default is 1.</p></dd></dl><h4 class="refsect2">Examples</h4><p>Here are some short, commented scripts:</p><blockquote><pre class="code"># Change third and fourth quote to ( and ):/function/{s/"/(/3s/"/)/4}# Remove all quotes on a given line:/Title/s/"//g# Remove first colon and all quotes; print resulting lines:s/://ps/"//gp# Change first "if" but leave "ifdef" alone:/ifdef/!s/if/   if/</pre></blockquote></td></tr><tr><td valign="top">t</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</em>]]<tt class="literal">t</tt> [<em class="replaceable">label</tt></em>]</p><p><a name="IXT-10-123256" />Test if successful substitutions have been made on addressed lines,and if so, branch to the line marked by <tt class="literal">:</tt><em class="emphasis">label</em>(see <b class="emphasis-bold"><a href="#b">b</a></b> and <b class="emphasis-bold"><a href="#:">:</a></b>).  If<em class="emphasis">label</em> is not specified, control branches to the bottom ofthe script.The <tt class="literal">t</tt> command is like a case statement in the Cprogramming language or the various shell programming languages.You test each case:  when it's true, you exit the construct.</p><h4 class="refsect2">Example</h4><p>Suppose you want to fill empty fields of a database.  You have this:</p><blockquote><pre class="code">ID: 1   Name: greg   Rate: 45ID: 2   Name: daleID: 3</pre></blockquote><p>You want this:</p><blockquote><pre class="code">ID: 1   Name: greg   Rate: 45   Phone: ??ID: 2   Name: dale   Rate: ??   Phone: ??ID: 3   Name: ????   Rate: ??   Phone: ??</pre></blockquote><p>You need to test the number of fields already there.Here's the script (fields are tab-separated):</p><blockquote><pre class="code">#n/ID/{s/ID: .* Name: .* Rate: .*/&amp;   Phone: ??/pts/ID: .* Name: .*/&amp;   Rate: ??   Phone: ??/pts/ID: .*/&amp;   Name: ????   Rate: ??   Phone: ??/p}</pre></blockquote></td></tr><tr><td valign="top">w</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</em>]]<tt class="literal">w</tt><em class="replaceable"> file</tt></em></p><p><a name="IXT-10-123257" />Append contents of pattern space to <em class="emphasis">file</em>.  This action occurswhen the command is encountered rather than when the pattern space isoutput.  Exactly one spacemust separate the <tt class="literal">w</tt> and the filename.A maximum of 10 different files can be opened in a script.This command creates the file if it does not exist; if the fileexists, its contents are overwritten each time the scriptis executed.  Multiple write commands that direct output to thesame file append to the end of the file.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code"># Store tbl and eqn blocks in a file:/^\.TS/,/^\.TE/w troff_stuff/^\.EQ/,/^\.EN/w troff_stuff</pre></blockquote></td></tr><tr><td valign="top">x</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">x</tt></p><p><a name="IXT-10-123258" />Exchange the contents of the pattern space with the contents of thehold space.  See <b class="emphasis-bold"><a href="#h">h</a></b> for an example.</p></td></tr><tr><td valign="top">y</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</em>]]<tt class="literal">y/</tt><em class="replaceable">abc</em><tt class="literal">/</tt><em class="replaceable">xyz</tt></em><tt class="literal">/</tt></p><p><a name="IXT-10-123259" />Translate characters.  Change every instance of <em class="emphasis">a</em>to <em class="emphasis">x</em>, <em class="emphasis">b</em> to <em class="emphasis">y</em>, <em class="emphasis">c</em> to <em class="emphasis">z</em>, etc.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code"># Change item 1, 2, 3 to Item A, B, C .../^item [1-9]/y/i123456789/IABCDEFGHI/</pre></blockquote></td></tr></table><p><a name="IXT-10-123260" /><a name="IXT-10-123261" /><a name="IXT-10-123262" /><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch10_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch11_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">10.4. Group Summary of sed Commands</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">11. The awk Programming Language</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2003</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,88,96" href="../index.htm"><area shape="rect" coords="90,0,165,96" href="../upt/index.htm"><area shape="rect" coords="168,1,253,107" href="../mac/index.htm"><area shape="rect" coords="255,0,335,97" href="../korn/index.htm"><area shape="rect" coords="337,0,415,109" href="../unixnut/index.htm"><area shape="rect" coords="417,0,512,122" href="../sedawk/index.htm"><area shape="rect" coords="514,0,605,105" href="../lunix/index.htm"><area shape="rect" coords="611,2,694,121" href="../vi/index.htm"></map></body></html>

⌨️ 快捷键说明

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