📄 ch34_20.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.20 The sed Test Command </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:32Z"><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_19.htm"TITLE="34.19 Making Edits Everywhere Except... "><LINKREL="next"HREF="ch34_21.htm"TITLE="34.21 Uses of the sed Quit 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_19.htm"TITLE="34.19 Making Edits Everywhere Except... "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.19 Making Edits Everywhere Except... "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_21.htm"TITLE="34.21 Uses of the sed Quit Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.21 Uses of the sed Quit Command "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7982">34.20 The sed Test Command </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-39239"></A>The test command, <EMCLASS="emphasis">t</EM>, branches to a label (or the end of the script) if asuccessful substitution has been made on the currentlyaddressed line.It implies a conditional branch.Its syntax is as follows:</P><BLOCKQUOTECLASS="blockquote"><PCLASS="para">[<CODECLASS="replaceable"><I>address</I></CODE>]<CODECLASS="literal">t</CODE>[<CODECLASS="replaceable"><I>label</I></CODE>]</P></BLOCKQUOTE><PCLASS="para">If no <CODECLASS="replaceable"><I>label</I></CODE> is supplied, control falls throughto the end of the script.If <CODECLASS="replaceable"><I>label</I></CODE> is supplied,then execution resumes at the line following the label.</P><PCLASS="para">Let's look at a spelling corrector written by Greg Ubben.The script fixes common (in this example, silly) spelling goofs;the <EMCLASS="emphasis">t</EM> command tells about corrections that were made:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">hs/seperate/separate/gs/compooter/computer/gs/said editor/sed editor/gs/lable/label/gt changedb: changedpgs/.*/[WAS: &]/t</PRE></BLOCKQUOTE></P><PCLASS="para">First,<SPANCLASS="link"><CODECLASS="literal">h</CODE> (<ACLASS="linkend"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A>)</SPAN>holds a copy of the current input line.Then, if any of the four substitutions succeed, the command<CODECLASS="literal">t changed</CODE> branches to the corresponding label (<CODECLASS="literal">: changed</CODE>)at the end of the script.Otherwise, if no <CODECLASS="literal">s</CODE> succeeded, the <CODECLASS="literal">b</CODE> command restarts thescript on the next line (as always in <EMCLASS="emphasis">sed</EM>, the input line is printedbefore the script re-starts). </P><PCLASS="para">After the label, the script prints the current input line (the line witha spelling error - which, by now, has been corrected).Then<SPANCLASS="link"><CODECLASS="literal">g</CODE> (<ACLASS="linkend"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A>)</SPAN>gets the original uncorrected line.An <CODECLASS="literal">s</CODE> command brackets that line <CODECLASS="literal">[WAS: </CODE><CODECLASS="replaceable"><I>xxx</I></CODE><CODECLASS="literal">]</CODE>.Here's some sample output:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>sed -f sedscr afile</B></CODE>This is a separate test.[WAS: This is a seperate test.]I put a label on my computer![WAS: I put a lable on my compooter!]That's all for now.</PRE></BLOCKQUOTE></P><PCLASS="para">The final <CODECLASS="literal">t</CODE> in the script is a work-around for a bug in some versionsof <EMCLASS="emphasis">sed</EM>.Greg says "The <EMCLASS="emphasis">t</EM> flag is supposed to be reset after either the<EMCLASS="emphasis">t</EM> command is executed or a new line of input is read, but some versionsof <EMCLASS="emphasis">sed</EM> don't reset it on a new line of input.So I added a do-nothing <CODECLASS="literal">t</CODE> to make sure it's reset after the previousalways-true <CODECLASS="literal">s///</CODE>."Try the script without the extra <CODECLASS="literal">t</CODE>; if adding it makes thescript work right, your <EMCLASS="emphasis">sed</EM> has the bug and you might try a newversion, like GNU <EMCLASS="emphasis">gsed</EM>.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN>, <SPANCLASS="authorinitials">DD</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_19.htm"TITLE="34.19 Making Edits Everywhere Except... "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.19 Making Edits Everywhere Except... "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_21.htm"TITLE="34.21 Uses of the sed Quit Command "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.21 Uses of the sed Quit Command "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.19 Making Edits Everywhere Except... </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.21 Uses of the sed Quit 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 + -