📄 ch34_24.htm
字号:
><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">c</CODE></DT><DDCLASS="listitem"><PCLASS="para"><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout"><CODECLASS="literal">[</CODE><CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE><CODECLASS="literal">]c\</CODE><BR><CODECLASS="replaceable"><I>text</I></CODE></P></BLOCKQUOTE></P><PCLASS="para">Replace the lines selected by the address with <CODECLASS="replaceable"><I>text</I></CODE>.When a range of lines is specified, all lines as a group are replacedby a single copy of <CODECLASS="replaceable"><I>text</I></CODE>.The newline following each line of <CODECLASS="replaceable"><I>text</I></CODE> must be escaped bya backslash, except the last line.The contents of the pattern space are, in effect, deleted andno subsequent editing commands can be applied to it (or <CODECLASS="replaceable"><I>text</I></CODE>).</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># Replace first 100 lines in a file:1,100c\<ICLASS="lineannotation">...first replacement line</I>\<ICLASS="lineannotation">...second replacement line</I>\<ICLASS="lineannotation">...</I>\<ICLASS="lineannotation">...last replacement line</I></PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">d</CODE></DT><DDCLASS="listitem"><PCLASS="para"><CODECLASS="literal">[</CODE><CODECLASS="replaceable"><I>address1</I></CODE><CODECLASS="literal">][,</CODE><CODECLASS="replaceable"><I>address2</I></CODE><CODECLASS="literal">]d</CODE></P><PCLASS="para">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.See articles<ACLASS="xref"HREF="ch34_04.htm"TITLE="sed Addressing Basics ">34.4</A>and<ACLASS="xref"HREF="ch34_18.htm"TITLE="Multiline Delete ">34.18</A>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># delete all blank lines:/^$/d</PRE></BLOCKQUOTE></P></DD><DTCLASS="term"><CODECLASS="literal">D</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">D</CODE></P><PCLASS="para">Delete first part (up to embedded newline) of multiline pattern space createdby <EMCLASS="emphasis">N</EM> command and resume editing with first command inscript. If this command empties the pattern space, then a new lineof input is read, as if <EMCLASS="emphasis">d</EM> had been executed.See article <ACLASS="xref"HREF="ch34_18.htm"TITLE="Multiline Delete ">34.18</A>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># Strip multiple blank lines, leaving only one:/^$/{ N /^\n$/D}</PRE></BLOCKQUOTE></P></DD><DTCLASS="term"><CODECLASS="literal">g</CODE></DT><DDCLASS="listitem"><PCLASS="para"><CODECLASS="literal">[</CODE><CODECLASS="replaceable"><I>address1</I></CODE><CODECLASS="literal">][,</CODE><CODECLASS="replaceable"><I>address2</I></CODE><CODECLASS="literal">]g</CODE></P><PCLASS="para">Paste the contents of the hold space (see <EMCLASS="emphasis">h</EM> or <EMCLASS="emphasis">H</EM>) backinto the pattern space, wiping out the previous contents of the pattern space.See articles <ACLASS="xref"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A> and <ACLASS="xref"HREF="ch34_16.htm"TITLE="The Deliberate Scrivener ">34.16</A>.The example shows a simple way to copy lines.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para">This script collects all lines containing the word <CODECLASS="literal">Item:</CODE>and copies them to a place marker later in the file.The place marker is overwritten.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/Item:/H/<Replace this line with the item list>/g</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">G</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">G</CODE></P><PCLASS="para">Same as <EMCLASS="emphasis">g</EM>, except that the hold space is pasted below the addressinstead of overwriting it.The example shows a simple way to "cut and paste" lines.See articles <ACLASS="xref"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A> and <ACLASS="xref"HREF="ch34_16.htm"TITLE="The Deliberate Scrivener ">34.16</A>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para">This script collects all lines containing the word <CODECLASS="literal">Item:</CODE>and moves them after a place marker later in the file.The original <CODECLASS="literal">Item:</CODE> lines are deleted.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/Item:/{ H d}/Summary of items:/G</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">h</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">h</CODE>Copy the pattern space into the hold space, a special temporary buffer.The previous contents of the hold space are obliterated.You can use <EMCLASS="emphasis">h</EM> to save a line before editing it.See articles <ACLASS="xref"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A> and <ACLASS="xref"HREF="ch34_16.htm"TITLE="The Deliberate Scrivener ">34.16</A>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># Edit a line; print the change; replay the original/UNIX/{ h s/.* UNIX \(.*\) .*/\1:/ p x}</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">Sample input:</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">This describes the UNIX ls command.This describes the UNIX cp command.</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">Sample output:</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">ls:This describes the UNIX ls command.cp:This describes the UNIX cp command.</PRE></BLOCKQUOTE></P><PCLASS="para"> </P></DD><DTCLASS="term"><CODECLASS="literal">H</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">H</CODE></P><PCLASS="para">Append the contents of the pattern space (preceded by a newline)to the contents of the hold space. Even if the hold space is empty,<EMCLASS="emphasis">H</EM> still appends a newline. <EMCLASS="emphasis">H</EM> is like an incremental copy.See examples under <EMCLASS="emphasis">g</EM> and <EMCLASS="emphasis">G</EM>,also articles<ACLASS="xref"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A>and<ACLASS="xref"HREF="ch34_16.htm"TITLE="The Deliberate Scrivener ">34.16</A>. </P></DD><DTCLASS="term"><CODECLASS="literal">i</CODE></DT><DDCLASS="listitem"><PCLASS="para"><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout"><CODECLASS="literal">[</CODE><CODECLASS="replaceable"><I>address</I></CODE><CODECLASS="literal">]i\</CODE><BR><CODECLASS="replaceable"><I>text</I></CODE> </P></BLOCKQUOTE>Insert <CODECLASS="replaceable"><I>text</I></CODE> before each line matched by <CODECLASS="replaceable"><I>address</I></CODE>.(See <EMCLASS="emphasis">a</EM> for details on <CODECLASS="replaceable"><I>text</I></CODE>.)Article<ACLASS="xref"HREF="ch43_20.htm"TITLE="Displaying a troff Macro Definition ">43.20</A>shows a script that uses <EMCLASS="emphasis">i</EM>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/Item 1/i\The five items are listed below:</PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">l</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">l</CODE></P><PCLASS="para">List the contents of the pattern space, showing non-printingcharacters as<SPANCLASS="link">ASCII codes (<ACLASS="linkend"HREF="ch51_03.htm"TITLE="ASCII Characters: Listing and Getting Values ">51.3</A>, <ACLASS="linkend"HREF="ch25_07.htm"TITLE="Show Non-Printing Characters with cat -v or od -c ">25.7</A>)</SPAN>.Long lines are wrapped.</P></DD><DTCLASS="term"><CODECLASS="literal">n</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">n</CODE></P><PCLASS="para">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 <EMCLASS="emphasis">n</EM> instead of resuming at the topof the script.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para">In the<SPANCLASS="link"><EMCLASS="emphasis">ms</EM> macros (<ACLASS="linkend"HREF="ch43_14.htm"TITLE="nroff/troff and Macro Packages ">43.14</A>)</SPAN>,a section header occurs on the linebelow an <CODECLASS="literal">.NH</CODE> macro. To print all lines of header text, invoke thisscript with <EMCLASS="emphasis">sed -n</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.NH/{ n p}<ACLASS="indexterm"NAME="AUTOID-39783"></A></PRE></BLOCKQUOTE></P><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">N</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">N</CODE></P><PCLASS="para">Append next input line to contents of pattern space; the two lines areseparated by an embedded newline.(This command is designed to allow pattern matches across twolines.) Using <CODECLASS="literal">\n</CODE> to match the embedded newline, you can matchpatterns across multiple lines. See example under <EMCLASS="emphasis">D</EM>, also article<ACLASS="xref"HREF="ch34_15.htm"TITLE="Making Edits Across Line Boundaries ">34.15</A>.</P><PCLASS="para"><EMCLASS="emphasis">Examples</EM></P><PCLASS="para">Like previous example, but print <CODECLASS="literal">.NH</CODE> line as well as header title:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.NH/{ N p}</PRE></BLOCKQUOTE></P><PCLASS="para">Join two lines (replace newline with space):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.NH/{ N s/\n/ / p}</PRE></BLOCKQUOTE></P></DD><DTCLASS="term"><CODECLASS="literal">p</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">p</CODE></P><PCLASS="para">Print the addressed line(s). Unless the <EMCLASS="emphasis">-n</EM> command-lineoption is used, this command will cause duplicate lines to beoutput. Also, it is typically used before commands thatchange flow control (<EMCLASS="emphasis">d</EM>, <EMCLASS="emphasis">N</EM>, <EMCLASS="emphasis">b</EM>) and that mightprevent the current line from being output.See examples under <EMCLASS="emphasis">h</EM>, <EMCLASS="emphasis">n</EM>, and <EMCLASS="emphasis">N</EM>.</P></DD><DTCLASS="term"><CODECLASS="literal">P</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address1</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address2</I></CODE>]<CODECLASS="literal">P</CODE></P><PCLASS="para">Print first part (up to embedded newline) of multiline pattern createdby <EMCLASS="emphasis"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -