📄 ch34_16.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.16 The Deliberate Scrivener </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:26Z"><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_15.htm"TITLE="34.15 Making Edits Across Line Boundaries "><LINKREL="next"HREF="ch34_17.htm"TITLE="34.17 Searching for Patterns Split Across Lines "></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_15.htm"TITLE="34.15 Making Edits Across Line Boundaries "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.15 Making Edits Across Line Boundaries "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_17.htm"TITLE="34.17 Searching for Patterns Split Across Lines "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.17 Searching for Patterns Split Across Lines "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7833">34.16 The Deliberate Scrivener </A></H2><PCLASS="para">The operations of <EMCLASS="emphasis">sed</EM>'s most difficult commands - hold (<EMCLASS="emphasis">h</EM>or <EMCLASS="emphasis">H</EM>), get (<EMCLASS="emphasis">g</EM> or <EMCLASS="emphasis">G</EM>), and exchange(<EMCLASS="emphasis">x</EM>)-can be explained, somewhat fancifully, in terms of anextremely deliberate medieval scrivener or amanuensis toiling to makea copy of a manuscript. His work is bound by several spatialrestrictions: the original manuscript is displayed in one room; theset of instructions for copying the manuscript are stored in a middleroom; and the quill, ink, and folio are set up in yet anotherroom. The original manuscript as well as the set of instructions arewritten in stone and cannot be moved about. The dutiful scrivener,being sounder of body than mind, is able to make a copy by going fromroom to room, working on only one line at a time. Entering the roomwhere the original manuscript is, he removes from his robes a scrap ofpaper to take down the first line of the manuscript. Then he moves tothe room containing the list of editing instructions. He reads eachinstruction to see if it applies to the single line he has scribbleddown.</P><PCLASS="para">Each instruction, written in special notation, consists of two parts:a <EMCLASS="emphasis">pattern</EM> and a <EMCLASS="emphasis">procedure</EM>. The scrivener reads the firstinstruction and checks the pattern against his line. If there is nomatch, he doesn't have to worry about the procedure, so he goes to thenext instruction. If he finds a match, then the scrivener follows theaction or actions specified in the procedure.</P><PCLASS="para">He makes the edit on his piece of paper before trying to match thepattern in the next instruction. Remember, the scrivener has to readthrough a series of instructions, and he reads all of them, not justthe first instruction that matches the pattern. Because he makes hisedits as he goes, he is always trying to match the latest versionagainst the next pattern; he doesn't remember the original line.</P><PCLASS="para">When he gets to the bottom of the list of instructions, and has madeany edits that were necessary on his piece of paper, he goes into thenext room to copy out the line. (He doesn't need to be told to printout the line.) After that is done, he returns to the first room andtakes down the next line on a new scrap of paper. When he goes to thesecond room, once again he reads every instruction from first to lastbefore leaving.</P><PCLASS="para">This is what he normally does, that is, unless he is told otherwise.For instance, before he starts, he can be told <EMCLASS="emphasis">not</EM> to write outevery line (the <EMCLASS="emphasis">-n</EM> option). In this case, he must wait for aninstruction that tells him to print (<EMCLASS="emphasis">p</EM>). If he does not getthat instruction, he throws away his piece of paper and startsover. By the way, regardless of whether or not he is told to write outthe line, he always gets to the last instruction on the list.</P><PCLASS="para">Let's look at other kinds of instructions the scrivener has tointerpret. First of all, an instruction can have zero, one, or twopatterns specified:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">If no pattern is specified, then the same procedure is followed foreach line.</P></LI><LICLASS="listitem"><PCLASS="para">If there is only one pattern, he will follow the procedure for anyline matching the pattern.</P></LI><LICLASS="listitem"><PCLASS="para">If a pattern is followed by a <CODECLASS="literal">!</CODE>, then the procedure isfollowed for all lines that do <EMCLASS="emphasis">not</EM> match the pattern.</P></LI><LICLASS="listitem"><PCLASS="para">If two patterns are specified, the actions described in the procedureare performed on the first matching line and all succeeding linesuntil a line matches the second pattern.</P></LI></UL><PCLASS="para">The scrivener can work on only one line at a time, so you might wonderhow he handles a range of lines. Each time he goes through theinstructions, he tries to match only the first of two patterns. Now,after he has found a line that matches the first pattern, each timethrough with a new line he tries to match the second pattern. Heinterprets the second pattern as <CODECLASS="replaceable"><I>pattern</I></CODE><CODECLASS="literal">!</CODE>,so that the procedure is followed only if there is no match. When the secondpattern is matched, he starts looking again for the first pattern.</P><PCLASS="para">Each procedure contains one or more commands or <EMCLASS="emphasis">actions</EM>.Remember, if a pattern is specified with a procedure, the pattern mustbe matched before the procedure is executed. We have already shownmany of the usual commands that are similar to other editing commands.However, there are several highly unusual commands.</P><PCLASS="para">For instance, the <EMCLASS="emphasis">N</EM> command tells the scrivener to go, rightnow, and get another line, adding it to the same piece of paper. Thescrivener can be instructed to "hold" on to a single piece of scrappaper. The <EMCLASS="emphasis">h</EM> command tells him to make a copy of the line onanother piece of paper and put it in his pocket. The <EMCLASS="emphasis">x</EM> commandtells him to exchange the extra piece of paper in his pocket with theone in his hand. The <EMCLASS="emphasis">g</EM> command tells him to throw out the paperin his hand and replace it with the one in his pocket. The <EMCLASS="emphasis">G</EM>command tells him to append the line he is holding to the paper infront of him. If he encounters a <EMCLASS="emphasis">d</EM> command, he throws out thescrap of paper and begins again at the top of the list ofinstructions. A <EMCLASS="emphasis">D</EM> command has effect when he has beeninstructed to append two lines on his piece of paper. The <EMCLASS="emphasis">D</EM>command tells him to delete the first of those lines.</P><PCLASS="para">If you want the analogy converted back to computers, the first andlast rooms in this medieval manor are standard input and standardoutput. Thus, the original file is never changed. The line on thescrivener's piece of scrap paper is in the <EMCLASS="emphasis">pattern space</EM>; theline on the piece of paper that he holds in his pocket is in the<EMCLASS="emphasis">hold space</EM>. The hold space allows you to retain a duplicate of aline while you change the original in the pattern space.</P><PCLASS="para">Article<ACLASS="xref"HREF="ch34_17.htm"TITLE="Searching for Patterns Split Across Lines ">34.17</A>shows a practical application of the scrivener's work, a <EMCLASS="emphasis">sed</EM>program that searches for a particular phrase that might be splitacross two lines.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <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_15.htm"TITLE="34.15 Making Edits Across Line Boundaries "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.15 Making Edits Across Line Boundaries "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_17.htm"TITLE="34.17 Searching for Patterns Split Across Lines "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.17 Searching for Patterns Split Across Lines "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.15 Making Edits Across Line Boundaries </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.17 Searching for Patterns Split Across Lines </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 + -