📄 ch34_19.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.19 Making Edits Everywhere Except... </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:31Z"><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_18.htm"TITLE="34.18 Multiline Delete "><LINKREL="next"HREF="ch34_20.htm"TITLE="34.20 The sed Test Command "></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_18.htm"TITLE="34.18 Multiline Delete "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.18 Multiline Delete "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="SECT1"HREF="ch34_20.htm"TITLE="34.20 The sed Test Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.20 The sed Test Command "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7778">34.19 Making Edits Everywhere Except... </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-39195"></A>There are two ways in <EMCLASS="emphasis">sed</EM> to avoid specified portions of a documentwhile making the edits everywhere else.You can use the <EMCLASS="emphasis">!</EM> command to specify that the edit appliesonly to lines that <EMCLASS="emphasis">do not</EM> match the pattern. Anotherapproach is to use the<EMCLASS="emphasis">b</EM> (branch) command to skip over portions of the editing script.Let's look at an example. </P><PCLASS="para">As described in article<ACLASS="xref"HREF="ch43_21.htm"TITLE="Preprocessing troff Input with sed ">43.21</A>,we use <EMCLASS="emphasis">sed</EM> to preprocess the input to <EMCLASS="emphasis">troff</EM> so that double dashes (<CODECLASS="literal">--</CODE>)are converted automatically to em-dashes ( - ) and straight quotes (<CODECLASS="literal">""</CODE>) are converted to curly quotes ("").However, program examples in technical books are usually shownin a constant-width font that clearly shows each character as it appears on the computer screen.When typesetting a document, we don't want <EMCLASS="emphasis">sed</EM> to apply the sameediting rules within these examples as it doesto the rest of the document. For instance,straight quotes should not be replaced by curly quotes.</P><PCLASS="para">Because program examplesare set off by a pair of macros (something like .ESand .EE, for"Example Start" and"Example End"), we can use those as thebasis for exclusion.</P><PCLASS="para">So you can say:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.ES/,/^\.EE/!{ s/^"/``/ ... s/\\(em"/\\(em``/g}<ACLASS="indexterm"NAME="AUTOID-39213"></A></PRE></BLOCKQUOTE></P><PCLASS="para">All of the commands enclosed in braces (<CODECLASS="literal">{}</CODE>) willbe subject to the initial pattern address.</P><PCLASS="para">There is another way to accomplish the same thing.The <EMCLASS="emphasis">b</EM> command allows you to transfer control to another line inthe script that is marked with an optional label.Using this feature, you could write the above script like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.ES/,/^\.EE/bends/^"/``/ ...s/\\(em"/\\(em``/g:end</PRE></BLOCKQUOTE></P><PCLASS="para">A label consists of a colon (<CODECLASS="literal">:</CODE>), followed by up toseven characters.If the label is missing, the <EMCLASS="emphasis">b</EM> command branches to the end of the script.(In the example above, the label <CODECLASS="literal">end</CODE> was included just toshow how to use one, but a label is not really necessaryhere.)</P><PCLASS="para">The <EMCLASS="emphasis">b</EM> command is designed for flow controlwithin the script.It allows you to create subscriptsthat will only be applied to lines matching certainpatterns and will not be applied elsewhere.However, as in this case, it also provides a powerful wayto exempt part of the text from the action of asingle-level script.</P><PCLASS="para">The advantage of <EMCLASS="emphasis">b</EM> over <EMCLASS="emphasis">!</EM> for this applicationis that you can more easily specify multiple conditions to avoid.The <EMCLASS="emphasis">!</EM> command can be applied to asingle command or to the set of commands, enclosed inbraces, that immediately follows.On the otherhand, <EMCLASS="emphasis">b</EM> gives you almost unlimited control overmovement around the script.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_18.htm"TITLE="34.18 Multiline Delete "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.18 Multiline Delete "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_20.htm"TITLE="34.20 The sed Test Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.20 The sed Test Command "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.18 Multiline Delete </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">34.20 The sed Test Command </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -