📄 ch34_05.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.5 Order of Commands in a Script </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:14Z"><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_04.htm"TITLE="34.4 sed Addressing Basics "><LINKREL="next"HREF="ch34_06.htm"TITLE="34.6 One Thing at a Time "></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_04.htm"TITLE="34.4 sed Addressing Basics "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.4 sed Addressing Basics "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_06.htm"TITLE="34.6 One Thing at a Time "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.6 One Thing at a Time "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7976">34.5 Order of Commands in a Script </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-38495"></A>Combining a series of edits in a scriptcan have unexpected results.You might not think of the consequences one edit can have onanother.New users typically think that <EMCLASS="emphasis">sed</EM> appliesan individual editing command to all lines of input before applying the nextediting command.But the opposite is true.<EMCLASS="emphasis">sed</EM> appliesevery editing command to the first input line before reading the secondinput line and applying the editing script to it.Because <EMCLASS="emphasis">sed</EM>is always working with thelatest version of the original line,any edit that is made changes the line for subsequent commands.<EMCLASS="emphasis">sed</EM> doesn't retain the original.This means that a pattern that might have matched the originalinput line may no longer match the line after an edit hasbeen made.</P><PCLASS="para">Let's look at an example that uses the substitute command.Suppose someone quickly wrotethe following script to change <CODECLASS="literal">pig</CODE> to <CODECLASS="literal">cow</CODE> and <CODECLASS="literal">cow</CODE>to <CODECLASS="literal">horse</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">s/pig/cow/s/cow/horse/</PRE></BLOCKQUOTE></P><PCLASS="para">The first command would change <CODECLASS="literal">pig</CODE> to <CODECLASS="literal">cow</CODE> as expected.However, when the second command changed <CODECLASS="literal">cow</CODE> to <CODECLASS="literal">horse</CODE> onthe same line,it also changed the <CODECLASS="literal">cow</CODE> that had been a <CODECLASS="literal">pig</CODE>.So, where the inputfile contained pigs and cows, the output file has only horses!</P><PCLASS="para">This mistake is simply a problem of the order of the commands inthe script.Reversing the order of the commands - changing<CODECLASS="literal">cow</CODE> into <CODECLASS="literal">horse</CODE> before changing <CODECLASS="literal">pig</CODE> into <CODECLASS="literal">cow</CODE> - does the trick.</P><PCLASS="para">Some <EMCLASS="emphasis">sed</EM> commands change the flow through the script.For example, the<SPANCLASS="link"><EMCLASS="emphasis">N</EM> command (<ACLASS="linkend"HREF="ch34_15.htm"TITLE="Making Edits Across Line Boundaries ">34.15</A>)</SPAN>reads another lineinto the pattern space without removing the current line, so you cantest for patterns across multiple lines.Other commands tell <EMCLASS="emphasis">sed</EM> to exit beforereaching the bottom of the script or to go to a labeled command.<EMCLASS="emphasis">sed</EM> also maintains a second temporary buffer calledthe <EMCLASS="emphasis">hold space</EM>.You can copy the contentsof the pattern space to the hold space and retrieve it later.The commands that make use of the hold space are discussedin article <ACLASS="xref"HREF="ch34_13.htm"TITLE="Hold Space: The Set-Aside Buffer ">34.13</A>and other articles after it.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">sed & awk</CITE></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_04.htm"TITLE="34.4 sed Addressing Basics "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.4 sed Addressing Basics "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_06.htm"TITLE="34.6 One Thing at a Time "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.6 One Thing at a Time "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.4 sed Addressing Basics </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.6 One Thing at a Time </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 + -