📄 ch34_13.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.13 Hold Space: The Set-Aside Buffer </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:20Z"><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_12.htm"TITLE="34.12 Transformations on Text "><LINKREL="next"HREF="ch34_14.htm"TITLE="34.14 Transforming Part of a Line "></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_12.htm"TITLE="34.12 Transformations on Text "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.12 Transformations on Text "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_14.htm"TITLE="34.14 Transforming Part of a Line "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.14 Transforming Part of a Line "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7981">34.13 Hold Space: The Set-Aside Buffer </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-38733"></A><ACLASS="indexterm"NAME="UPT-ART-7981-IX-SED-EDITOR-HOLD-SPACE"></A>The <EMCLASS="emphasis">pattern space</EM> is a<SPANCLASS="link">buffer (<ACLASS="linkend"HREF="glossary.htm#UPT-ART-1010"TITLE="Glossary">52.9</A>)</SPAN>that contains thecurrent input line.There is also a set-aside buffer calledthe <EMCLASS="emphasis">hold space</EM>.The contents of the pattern spacecan be copied to the hold space, and the contents of the holdspace can be copied to the pattern space.A group of commands allows you to move databetween the hold space and the pattern space.The hold space is used for temporary storage, and that'sit.Individual commands can't address the hold spaceor alter its contents.</P><PCLASS="para">The most frequent use of the hold space is to haveit retain a duplicate of the current input line whileyou change the original in the pattern space.[It's also used as a way to do the "move" and "copy" commands that mosteditors have - but which <EMCLASS="emphasis">sed</EM> can't do directly because it'sdesigned for editing a stream of input text line-by-line. <EMCLASS="emphasis">-GU</EM> ]The commands that affect the hold space are:</P><TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><BCLASS="emphasis.bold">Hold</B></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">h</EM> or <EMCLASS="emphasis">H</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Copy or append contents of pattern space to hold space.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><BCLASS="emphasis.bold">Get</B></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">g </EM>or<EMCLASS="emphasis"> G</EM></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Copy or append contents of hold space to pattern space.</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><BCLASS="emphasis.bold">Exchange</B></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">x</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">Swap contents of hold space and pattern space.</TD></TR></TBODY></TABLE><PCLASS="para">Each of these commands can take an address thatspecifies a single line or a range of lines.The hold commands (<EMCLASS="emphasis">h</EM>,<EMCLASS="emphasis">H</EM> ) move data into the hold spaceand the get commands (<EMCLASS="emphasis">g</EM>,<EMCLASS="emphasis">G</EM> ) move data from the holdspace back into the pattern space.The difference between the lowercase and uppercaseversions of the same command is that the lowercasecommand overwrites the contents of the target buffer,while the uppercase command appends to the existing contents.The hold command replaces the contents of the hold space withthe contents of the pattern space.The get command replacesthe contents of the pattern space with the contents of thehold space.</P><PCLASS="para">The Hold command puts a newline followed by the contents ofthe pattern space after the contents of the hold space.(The newline is appended to the hold space even if the holdspace is empty.)The Get command puts a newline followed by thecontents of the hold space after the contents of thepattern space.</P><PCLASS="para">The exchange command (<EMCLASS="emphasis">x</EM>) swaps the contents of the two buffers.It has no side effects on either buffer.</P><PCLASS="para">Here's an example to illustrate puttinglines into the hold space and retrieving them later.We are going to write a script that reads a particular HTML file andcopies all headings to the end of the file for a summary.The headings we want start with <CODECLASS="literal"><H1></CODE> or <CODECLASS="literal"><H2></CODE>.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">...<BODY><H1>Introduction</H1>The blah blah blah<H1>Background of the Project</H1> ...<H2>The First Year</H2> ...<H2>The Second Year</H2> ...</BODY></PRE></BLOCKQUOTE></P><PCLASS="para">The object is to copy those headings into the hold space as <EMCLASS="emphasis">sed</EM>reads them.When <EMCLASS="emphasis">sed</EM> reaches the end of the body (at the <CODECLASS="literal"></BODY></CODE> tag),output <CODECLASS="literal">Summary:</CODE>;then output the saved tags, without the heading tags(<CODECLASS="literal"><H1></CODE> or <CODECLASS="literal"><H2></CODE>).</P><PCLASS="para">Look at the script:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">/^<H[12]>/H/^<\/BODY>/ { i\<STRONG>Summary:</STRONG> x G s/<\/*H[12]>//g}</PRE></BLOCKQUOTE></P><PCLASS="para">Any line matching <CODECLASS="literal"><H1></CODE> or <CODECLASS="literal"><H2></CODE> is added to the holdspace.(All those lines are also printed; that's the default in <EMCLASS="emphasis">sed</EM> unlesslines have been deleted.)The last part of the script watches for the <CODECLASS="literal"></BODY></CODE> tag.When it's reached, sed inserts the <CODECLASS="literal">Summary:</CODE> heading.Then the script uses <CODECLASS="literal">x</CODE> to exchange the pattern space (which hasthe <CODECLASS="literal"></BODY></CODE> tag) with the saved headers from the hold space.Now the pattern space has the saved headers.Next, a <CODECLASS="literal">G</CODE> adds the <CODECLASS="literal"></BODY></CODE> tag to the end of the headersin the pattern space.Finally, a substitute command strips the <CODECLASS="literal"><H1></CODE>, <CODECLASS="literal"></H1></CODE>,<CODECLASS="literal"><H2></CODE>, and <CODECLASS="literal"></H2></CODE> tags.At the end of the script, the pattern space is printed by default.</P><PCLASS="para">The sequence of <EMCLASS="emphasis">x</EM> followed by <EMCLASS="emphasis">G</EM> is a way to find a matchingline - in this case, <CODECLASS="literal"></BODY></CODE>-and insert the contents of the holdspace before the matched line.In other words, it's like an <EMCLASS="emphasis">i</EM> command that inserts the hold spaceat the current line.</P><PCLASS="para">The script could do more cleanup and formatting.For instance, it could make the saved headings into a list with <CODECLASS="literal"><UL></CODE>and <CODECLASS="literal"><LI></CODE>.But this example is mostly about the hold space.</P><PCLASS="para">Here's the result of running the script on the sample file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>sed -f sedscr report.html</B></CODE> ...<BODY><H1>Introduction</H1>The blah blah blah<H1>Background of the Project</H1> ...<H2>The First Year</H2> ...<H2>The Second Year</H2> ...<STRONG>Summary:</STRONG>IntroductionBackground of the ProjectThe First YearThe Second Year</BODY></PRE></BLOCKQUOTE></P><PCLASS="para">For other scripts that use the hold space, see articles<ACLASS="xref"HREF="ch34_17.htm"TITLE="Searching for Patterns Split Across Lines ">34.17</A>and<ACLASS="xref"HREF="ch25_12.htm"TITLE="Double Space, Triple Space ... ">25.12</A>.For a fanciful analogy that makes clear how it works, see article<ACLASS="xref"HREF="ch34_16.htm"TITLE="The Deliberate Scrivener ">34.16</A>.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN>, <SPANCLASS="authorinitials">JP</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_12.htm"TITLE="34.12 Transformations on Text "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.12 Transformations on Text "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_14.htm"TITLE="34.14 Transforming Part of a Line "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.14 Transforming Part of a Line "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.12 Transformations on Text </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.14 Transforming Part of a Line </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 + -