📄 ch10_05.htm
字号:
<html><head><title>Alphabetical Summary of sed Commands (UNIX in a Nutshell: System V Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Arnold Robbins" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="1-56592-427-4" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="UNIX in a Nutshell: System V Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Book Title" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><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" /><td align="right" valign="top" width="228"><a href="ch11_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">10.5. Alphabetical Summary of sed Commands</h2><table border="1" cellpadding="5"><tr><td valign="top">#</td><td><p>#</p><p>Begin a comment in a <tt class="literal">sed</tt> script. Valid only as the firstcharacter of the first line.(Some versions allow comments anywhere, but it is better not torely on this.)If the first line of the script is <tt class="literal">#n</tt>,<tt class="literal">sed</tt> behaves as if<tt class="literal">-n</tt> had been specified.<a name="IXT-10-123233" /><a name="IXT-10-123234" /></p></td></tr><tr><td valign="top">:</td><td><p><tt class="literal">:</tt><em class="replaceable"><tt>label</tt></em></p><p><a name="IXT-10-123235" />Label a line in the script for transfer of control by <tt class="literal">b</tt> or <tt class="literal">t</tt>.<em class="emphasis">label</em>may contain up to seven characters.<a name="IXT-10-123236" /></p></td></tr><tr><td valign="top">=</td><td><p>[<tt class="literal">/</tt><em class="replaceable"><tt>pattern</tt></em><tt class="literal">/</tt>]<tt class="literal">=</tt></p><p><a name="IXT-10-123237" />Write to standard output the line number of each line addressed by<em class="emphasis">pattern</em>.</p></td></tr><tr><td valign="top">a</td><td><p>[<em class="replaceable"><tt>address</em>]<tt class="literal">a\</tt><br><em class="replaceable">text</tt></em></p><p><a name="IXT-10-123238" />Append <em class="emphasis">text</em> following each line matched by<em class="emphasis">address</em>. If <em class="emphasis">text</em>goes over more than one line, newlines must be “hidden” by precedingthem with a backslash. The <em class="emphasis">text</em> is terminated by the firstnewline that is not hidden in this way. The <em class="emphasis">text</em> isnot available in the pattern space, and subsequent commandscannot be applied to it. The results of this commandare sent to standard output when the list of editing commands is finished,regardless of what happens to the current line in the pattern space.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code">$a\This goes after the last line in the file\(marked by $). This text is escaped at the\end of each line, except for the last one.</pre></blockquote></td></tr><tr><td valign="top">b</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</em>]]<tt class="literal">b</tt>[<em class="replaceable">label</tt></em>]</p><p><a name="IXT-10-123239" />Transfer control unconditionally to <tt class="literal">:</tt><em class="emphasis">label</em> elsewhere in script.That is, the command followingthe <em class="emphasis">label</em> is the next command applied to the current line.If no <em class="emphasis">label</em> is specified, control falls throughto the end of the script, so no more commands are appliedto the current line.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code"># Ignore tbl tables; resume script after TE:/^\.TS/,/^\.TE/b</pre></blockquote></td></tr><tr><td valign="top">c</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</em>]]<tt class="literal">c\</tt><br><em class="replaceable">text</tt></em></p><p><a name="IXT-10-123240" />Replace (change) the lines selected by the address(es) with <em class="emphasis">text</em>.(See <b class="emphasis-bold"><a href="#a">a</a></b> for details on <em class="emphasis">text</em>.) When a range of lines is specified, all lines as a group are replacedby a single copy of <em class="emphasis">text</em>.The contents of the pattern space are, in effect, deleted andno subsequent editing commands can be applied to the pattern space (or to<em class="emphasis">text</em>).</p><h4 class="refsect2">Example</h4><blockquote><pre class="code"># Replace first 100 lines in a file:1,100c\\<First 100 names to be supplied></pre></blockquote></td></tr><tr><td valign="top">d</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">d</tt></p><p><a name="IXT-10-123241" />Delete the addressed line (or lines) from the pattern space. Thus, the line is not passed to standardoutput. A new line of input is read, and editing resumes with the firstcommand in the script.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code"># delete all blank lines:/^$/d</pre></blockquote></td></tr><tr><td valign="top">D</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">D</tt></p><p><a name="IXT-10-123242" />Delete first part (up to embedded newline) of multiline pattern space createdby <tt class="literal">N</tt> command and resume editing with first command inscript. If this command empties the pattern space, a new lineof input is read, as if the <tt class="literal">d</tt> command had been executed.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code"># Strip multiple blank lines, leaving only one:/^$/{N/^\n$/D}</pre></blockquote></td></tr><tr><td valign="top">g</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">g</tt></p><p><a name="IXT-10-123243" />Paste the contents of the hold space(see <b class="emphasis-bold"><a href="#h">h</a></b> and <b class="emphasis-bold"><a href="#H">H</a></b>) backinto the pattern space, wiping out the previous contents of the pattern space.The Example shows a simple way to copy lines.</p><h4 class="refsect2">Example</h4><p>This script collects all lines containing the word <em class="emphasis">Item:</em>and copies them to a place marker later in the file.The place marker is overwritten:</p><blockquote><pre class="code">/Item:/H/<Replace this line with the item list>/g</pre></blockquote></td></tr><tr><td valign="top">G</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">G</tt></p><p><a name="IXT-10-123244" />Same as <tt class="literal">g</tt>, except that a newline and thethe hold space are pasted to the end ofthe pattern spaceinstead of overwriting it.The Example shows a simple way to “cut and paste” lines.</p><h4 class="refsect2">Example</h4><p>This script collects all lines containing the word <em class="emphasis">Item:</em>and moves them after a place marker later in the file.The original <em class="emphasis">Item:</em> lines are deleted.</p><blockquote><pre class="code">/Item:/{Hd}/Summary of items:/G</pre></blockquote></td></tr><tr><td valign="top">h</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">h</tt></p><p><a name="IXT-10-123245" />Copy the pattern space into the hold space, a special temporary buffer.The previous contents of the hold space are obliterated.You can use <tt class="literal">h</tt> to save a line before editing it.</p><h4 class="refsect2">Example</h4><blockquote><pre class="code"># Edit a line; print the change; replay the original/Unix/{hs/.* Unix \(.*\) .*/\1:/px}</pre></blockquote><p>Sample input:</p><blockquote><pre class="code">This describes the Unix ls command.This describes the Unix cp command.</pre></blockquote><p>Sample output:</p><blockquote><pre class="code">ls:This describes the Unix ls command.cp:This describes the Unix cp command.</pre></blockquote></td></tr><tr><td valign="top">H</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">H</tt></p><p><a name="IXT-10-123246" />Append a newline and then the contents of the pattern spaceto the contents of the hold space. Even if the hold space is empty,<tt class="literal">H</tt> still appends a newline. <tt class="literal">H</tt> is like an incremental copy.See examples under <b class="emphasis-bold"><a href="#g">g</a></b> and <b class="emphasis-bold"><a href="#G">G</a></b>.</p></td></tr><tr><td valign="top">i</td><td><p>[<em class="replaceable"><tt>address</em>]<tt class="literal">i\</tt></p><p><em class="replaceable">text</tt></em> </p><p><a name="IXT-10-123247" />Insert <em class="emphasis">text</em> before each line matched by <em class="emphasis">address</em>. (See <b class="emphasis-bold"><a href="#a">a</a></b> for details on <em class="emphasis">text</em>.) </p><h4 class="refsect2">Example</h4><blockquote><pre class="code">/Item 1/i\The five items are listed below:</pre></blockquote></td></tr><tr><td valign="top">l</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">l</tt></p><p><a name="IXT-10-123248" />List the contents of the pattern space, showing nonprintingcharacters as ASCII codes. Long lines are wrapped.</p></td></tr><tr><td valign="top">n</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">n</tt></p><p><a name="IXT-10-123249" />Read next line of input into pattern space. The current line is sent tostandard output, and the next line becomes the current line.Control passes to the command following <tt class="literal">n</tt> instead of resuming at the topof the script.</p><h4 class="refsect2">Example</h4><p>In the <em class="emphasis">ms</em> macros, a section header occurs on the linebelow an <tt class="literal">.NH</tt> macro. To print all lines of header text, invoke thisscript with <tt class="literal">sed -n</tt>:</p><blockquote><pre class="code">/^\.NH/{np}</pre></blockquote></td></tr><tr><td valign="top">N</td><td><p>[<em class="replaceable"><tt>address1</em>[<tt class="literal">,</tt><em class="replaceable">address2</tt></em>]]<tt class="literal">N</tt></p><p><a name="IXT-10-123250" />Append next input line to contents of pattern space; the new line isseparated from the previous contents of the pattern space by a newline. (This command is designed to allow pattern matches across two
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -