📄 ch34_08.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.8 Newlines in a sed 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_07.htm"TITLE="34.7 Delimiting a Regular Expression "><LINKREL="next"HREF="ch34_09.htm"TITLE="34.9 Referencing the Search String in a Replacement "></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_07.htm"TITLE="34.7 Delimiting a Regular Expression "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.7 Delimiting a Regular Expression "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_09.htm"TITLE="34.9 Referencing the Search String in a Replacement "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.9 Referencing the Search String in a Replacement "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7773">34.8 Newlines in a sed Replacement </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-38579"></A>The backslash (<CODECLASS="literal">\</CODE>)in the replacement string of the <EMCLASS="emphasis">sed</EM> substitutioncommand is generally used to escape other metacharacters,but it is also used to include a newline in a replacement string.</P><PCLASS="para">Given the following input line where each item is separatedby a tab:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">Column1 Column2 Column3 Column4</PRE></BLOCKQUOTE></P><PCLASS="para">we can replace the second tab character on each line with a newline character:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch34_11.htm"TITLE="34.11 Search & Replacement: One Match Among Many ">2</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">s/<KBDCLASS="keycap">[TAB]</KBD>/\/2 </PRE></TD></TR></TABLE></P><PCLASS="para">Note that no spaces are permitted after the backslash. This script produces the following result:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">Column1 Column2Column3 Column4</PRE></BLOCKQUOTE></P><PCLASS="para">Another example comes from the conversion of a file for <EMCLASS="emphasis">troff</EM> to an ASCII input format for Ventura Publisher(TM). It converts the following line for <EMCLASS="emphasis">troff</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">.Ah "Major Heading"</PRE></BLOCKQUOTE></P><PCLASS="para">to a similar line for Ventura:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">@A HEAD = Major Heading</PRE></BLOCKQUOTE></P><PCLASS="para"> The twist in this problem is that the line needs to be preceded and followedby a blank line. It is an example of writing a multiline replacementstring:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^\.Ah/{s/\.Ah */\\@A HEAD = /s/"//gs/$/\/ }</PRE></BLOCKQUOTE></P><PCLASS="para">The first substitute command replaces <CODECLASS="literal">.Ah</CODE> with two newlines and <CODECLASS="literal">@A HEAD = </CODE>.Each backslash at the end of the line is necessary to escape the newline. The second substitution removes the quotation marks. The lastcommand matches the end of line in the pattern space (not the embeddednewlines) and adds a newline after it.</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_07.htm"TITLE="34.7 Delimiting a Regular Expression "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.7 Delimiting a Regular Expression "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_09.htm"TITLE="34.9 Referencing the Search String in a Replacement "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.9 Referencing the Search String in a Replacement "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.7 Delimiting a Regular Expression </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.9 Referencing the Search String in a Replacement </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 + -