📄 ch34_09.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.9 Referencing the Search String in a Replacement </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:16Z"><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_08.htm"TITLE="34.8 Newlines in a sed Replacement "><LINKREL="next"HREF="ch34_10.htm"TITLE="34.10 Referencing Portions of a Search String "></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_08.htm"TITLE="34.8 Newlines in a sed Replacement "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.8 Newlines in a sed Replacement "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_10.htm"TITLE="34.10 Referencing Portions of a Search String "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.10 Referencing Portions of a Search String "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7774">34.9 Referencing the Search String in a Replacement </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-38616"></A>As a metacharacter, the ampersand (<CODECLASS="literal">&</CODE>) represents the extent of the pattern match, not the line that was matched.For instance, you might use it to match a word and surround it with <EMCLASS="emphasis">troff</EM> requests.The following example surrounds a word with point-size requests:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">s/UNIX/\\s-2&\\s0/g</PRE></BLOCKQUOTE></P><PCLASS="para">Because backslashes are also replacement metacharacters, two backslashes are necessary to output a single backslash.The <CODECLASS="literal">&</CODE> in the replacement string refers to <CODECLASS="literal">UNIX</CODE>. If the input line is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">on the UNIX Operating System.</PRE></BLOCKQUOTE></P><PCLASS="para">then the substitute command produces:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">on the \s-2UNIX\s0 Operating System.</PRE></BLOCKQUOTE></P><PCLASS="para">The ampersand is particularly useful when the regular expressionmatches variations of a word. It allows you tospecify a variable replacement stringthat corresponds to what was actually matched.For instance, let's say that you wanted to surround withparentheses any cross reference to a numbered sectionin a document. In other words, any reference such as <CODECLASS="literal">See Section 1.4</CODE> or<CODECLASS="literal">See Section 12.9</CODE> should appear in parentheses, as <CODECLASS="literal">(SeeSection 12.9)</CODE>.A regular expression can match the different combination of numbers, so we use <CODECLASS="literal">&</CODE> in the replacementstring and surround whatever was matched: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">s/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/</PRE></BLOCKQUOTE></P><PCLASS="para">The ampersand makes it possible to reference the entire match in the replacement string.</P><PCLASS="para">In the next example, the backslash is used to escapethe ampersand, which appearsliterally in the replacement section:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">s/ORA/O'Reilly & Associates, Inc./g</PRE></BLOCKQUOTE></P><PCLASS="para">It's easy to forget about the ampersand appearing literallyin the replacement string. If we had not escaped it in thisexample, the output would have been <CODECLASS="literal">O'Reilly ORA Associates, Inc.</CODE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">sed & awk</CITE>, Chapter 5</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_08.htm"TITLE="34.8 Newlines in a sed Replacement "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.8 Newlines in a sed Replacement "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_10.htm"TITLE="34.10 Referencing Portions of a Search String "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.10 Referencing Portions of a Search String "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.8 Newlines in a sed Replacement </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.10 Referencing Portions of a Search String </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 + -