📄 ch34_24.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.24 Quick Reference: sed </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:47:38Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch34_01.htm"TITLE="34. The sed Stream Editor"><LINKREL="prev"HREF="ch34_23.htm"TITLE="34.23 sed Newlines, Quoting, and Backslashes in a Shell Script"><LINKREL="next"HREF="ch35_01.htm"TITLE="35. You Can't Quite Call This Editing"></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_23.htm"TITLE="34.23 sed Newlines, Quoting, and Backslashes in a Shell Script"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.23 sed Newlines, Quoting, and Backslashes in a Shell Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 34<BR>The sed Stream Editor</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="CHAPTER"HREF="ch35_01.htm"TITLE="35. You Can't Quite Call This Editing"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 35. You Can't Quite Call This Editing"BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2850">34.24 Quick Reference: sed </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-2850-IX-SED-EDITOR-COMMAND-LIST"></A>How <EMCLASS="emphasis">sed</EM> operates:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Each line of input is copied into a pattern space.</P></LI><LICLASS="listitem"><PCLASS="para">Editing commands may be given on the command line (if more than one, usea <EMCLASS="emphasis">-e</EM> option before each command) and/or in script files named after<EMCLASS="emphasis">-f</EM> options.All editing commands are applied in order to each line of input.</P></LI><LICLASS="listitem"><PCLASS="para">Editing commands are applied to all lines (globally) unless lineaddressing restricts the lines affected.</P></LI><LICLASS="listitem"><PCLASS="para">If a command changes the input, subsequent command-addresses will be applied to the current line in the pattern space, not the original input line.</P></LI><LICLASS="listitem"><PCLASS="para">The original input file is unchanged; editing commands modify a copy of the original input line.The copy is sent to<SPANCLASS="link">standard output (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>unless the <EMCLASS="emphasis">-n</EM> option was used; standard output can be<SPANCLASS="link">redirected to a file (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>, <ACLASS="linkend"HREF="ch34_03.htm"TITLE="Testing and Using a sed Script: checksed, runsed ">34.3</A>)</SPAN>.</P></LI></UL><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2850-SECT-1.1">34.24.1 Syntax of sed Commands </A></H3><PCLASS="para"><EMCLASS="emphasis">sed</EM> commands have the general form:</P><BLOCKQUOTECLASS="blockquote"><PCLASS="para">[<CODECLASS="replaceable"><I>address</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address</I></CODE>][<CODECLASS="literal">!</CODE>]<CODECLASS="replaceable"><I>command </I></CODE>[<CODECLASS="replaceable"><I>arguments</I></CODE>]</P></BLOCKQUOTE><PCLASS="para"><EMCLASS="emphasis">sed</EM> commands consist of <CODECLASS="replaceable"><I>addresses</I></CODE> and editing <CODECLASS="replaceable"><I>commands</I></CODE>.<CODECLASS="replaceable"><I>commands</I></CODE> consist of a single letter or symbol; they aredescribed later, alphabetically and by group.<CODECLASS="replaceable"><I>arguments</I></CODE> include the label supplied to <EMCLASS="emphasis">b</EM> or <EMCLASS="emphasis">t</EM>, thefilename supplied to <EMCLASS="emphasis">r</EM> or <EMCLASS="emphasis">w</EM>, and the substitution flagsfor <EMCLASS="emphasis">s</EM>.<CODECLASS="replaceable"><I>addresses</I></CODE> are described below.Elements in <CODECLASS="literal">[</CODE>brackets<CODECLASS="literal">]</CODE> are optional; don't type thebrackets.</P><PCLASS="para">Braces (<CODECLASS="literal">{}</CODE>) are used in <EMCLASS="emphasis">sed</EM> to nest one address inside another orto apply multiple commands at the same address:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">[<CODECLASS="replaceable"><I>address</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address</I></CODE>]{ <CODECLASS="replaceable"><I>command1 command2</I></CODE>}</PRE></BLOCKQUOTE></P><PCLASS="para">The left curly brace (<CODECLASS="literal">{</CODE>) is a command that starts a group ofother <EMCLASS="emphasis">sed</EM> commands.The group ends with a right curly brace (<CODECLASS="literal">}</CODE>).Commands within the braces may be spread across multiple lines, as shownabove.Or commands may be on the same line, with a semicolon(<CODECLASS="literal">;</CODE>) after each command (including the last command on aline) - as in:</P><BLOCKQUOTECLASS="blockquote"><PCLASS="para">[<CODECLASS="replaceable"><I>address</I></CODE>][<CODECLASS="literal">,</CODE><CODECLASS="replaceable"><I>address</I></CODE>]<CODECLASS="literal">{</CODE><CODECLASS="replaceable"><I>command1</I></CODE><CODECLASS="literal">; </CODE>...<CODECLASS="replaceable"><I>commandN</I></CODE><CODECLASS="literal">; }</CODE></P></BLOCKQUOTE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2850-SECT-1.2">34.24.2 Pattern Addressing </A></H3><PCLASS="para">A <EMCLASS="emphasis">sed</EM> command can specify zero, one, or two addresses.An address can be a line number, the symbol <CODECLASS="literal">$</CODE> (for last line),or a regular expression enclosed in slashes (<CODECLASS="literal">/</CODE><CODECLASS="replaceable"><I>pattern</I></CODE><CODECLASS="literal">/</CODE>).Regular expressions are described in <ACLASS="xref"HREF="ch26_01.htm"TITLE="Regular Expressions (Pattern Matching)">Chapter 26, Regular Expressions (Pattern Matching)</A>.Additionally, <CODECLASS="literal">\n</CODE> canbe used to match any newline in thepattern space (resulting from the <EMCLASS="emphasis">N</EM> command), but not thenewline at the end of the pattern space.See article<ACLASS="xref"HREF="ch34_04.htm"TITLE="sed Addressing Basics ">34.4</A>.</P><TABLECLASS="informaltable"><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">If the command specifies:</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Then it is applied to:</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">No address</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Each input line.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">One address</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Any line matching the address. Some commands accept only one address: <EMCLASS="emphasis">a</EM>, <EMCLASS="emphasis">i</EM>, <EMCLASS="emphasis">r</EM>, <EMCLASS="emphasis">q</EM>, and <CODECLASS="literal">=</CODE>.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Two comma-separated addresses</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">First matching line and all succeeding lines up to and including a line matching the second address. Repeat for each matching range in the text.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">An address followed by <CODECLASS="literal">!</CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">All lines that do <EMCLASS="emphasis">not</EM> match the address.</P></TD></TR></TBODY></TABLE><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="UPT-ART-2850-SECT-1.2.1">34.24.2.1 Examples </A></H4><PCLASS="para">Substitute on all lines (all occurrences):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">s/xx/yy/g</PRE></BLOCKQUOTE></P><PCLASS="para">Delete lines containing <CODECLASS="literal">BSD</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/BSD/d</PRE></BLOCKQUOTE></P><PCLASS="para">Print the lines between each pair of<CODECLASS="literal">BEGIN</CODE> and <CODECLASS="literal">END</CODE>, inclusive:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^BEGIN/,/^END/p</PRE></BLOCKQUOTE></P><PCLASS="para">Delete any line that doesn't contain <CODECLASS="literal">SAVE</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/SAVE/!d</PRE></BLOCKQUOTE></P><PCLASS="para">Substitute on all lines, except between <CODECLASS="literal">BEGIN</CODE> and <CODECLASS="literal">END</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/BEGIN/,/END/!s/xx/yy/g</PRE></BLOCKQUOTE></P></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2850-SECT-1.3">34.24.3 Alphabetical Summary of sed Commands </A></H3><PCLASS="para"></P><DLCLASS="variablelist"><DTCLASS="term"><CODECLASS="literal">#</CODE></DT><DDCLASS="listitem"><PCLASS="para">Begin a comment in a <EMCLASS="emphasis">sed</EM> script.If the first such line is exactly <CODECLASS="literal">#n</CODE>, <EMCLASS="emphasis">sed</EM> setsits <EMCLASS="emphasis">-n</EM> command-line option.</P></DD><DTCLASS="term"><CODECLASS="literal">:</CODE></DT><DDCLASS="listitem"><PCLASS="para"><CODECLASS="literal">:</CODE><CODECLASS="replaceable"><I>label</I></CODE></P><PCLASS="para">Label a line in the script for the transfer of control by <EMCLASS="emphasis">b</EM> or <EMCLASS="emphasis">t</EM>. <CODECLASS="replaceable"><I>label</I></CODE>may contain up to seven characters.</P></DD><DTCLASS="term"><CODECLASS="literal">=</CODE></DT><DDCLASS="listitem"><PCLASS="para">[<CODECLASS="replaceable"><I>address</I></CODE>]<CODECLASS="literal">=</CODE></P><PCLASS="para">Write to standard output the line number of each line addressed.</P></DD><DTCLASS="term"><CODECLASS="literal">a</CODE></DT><DDCLASS="listitem"><PCLASS="para"><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout"><CODECLASS="literal">[</CODE><CODECLASS="replaceable"><I>address</I></CODE><CODECLASS="literal">]a\</CODE><BR><CODECLASS="replaceable"><I>text</I></CODE></P></BLOCKQUOTE></P><PCLASS="para">Append <CODECLASS="replaceable"><I>text</I></CODE> following each line matched by<CODECLASS="replaceable"><I>address</I></CODE>.If there is more than one line of <CODECLASS="replaceable"><I>text</I></CODE>,all newlines except the last must be "hidden" by precedingthem with a backslash. <CODECLASS="replaceable"><I>text</I></CODE> will be terminated by the firstnewline that is not hidden in this way. <CODECLASS="replaceable"><I>text</I></CODE> 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.(There's an example in article<ACLASS="xref"HREF="ch43_22.htm"TITLE="Converting Text Files to PostScript ">43.22</A>,among others.)</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$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><ACLASS="indexterm"NAME="AUTOID-39560"></A></P></DD><DTCLASS="term"><CODECLASS="literal">b</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">b[</CODE><CODECLASS="replaceable"><I>label</I></CODE><CODECLASS="literal">]</CODE></P><PCLASS="para">Transfer control unconditionally to <CODECLASS="literal">:</CODE><CODECLASS="replaceable"><I>label</I></CODE> elsewhere in script.That is, the command followingthe <CODECLASS="replaceable"><I>label</I></CODE> is the next command applied to the current line.If no <CODECLASS="replaceable"><I>label</I></CODE> is specified, control falls throughto the end of the script, so no more commands are appliedto the current line. See articles <ACLASS="xref"HREF="ch34_19.htm"TITLE="Making Edits Everywhere Except... ">34.19</A> and<ACLASS="xref"HREF="ch34_17.htm"TITLE="Searching for Patterns Split Across Lines ">34.17</A>.</P><PCLASS="para"><EMCLASS="emphasis">Example</EM></P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># Ignore tbl tables; resume script after TE:/^\.TS/,/^\.TE/b</PRE></BLOCKQUOTE></P
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -