34.13 Hold Space: The Set-Aside BufferThe pattern space is a buffer (52.9 ) that contains the current input line. There is also a set-aside buffer called the hold space . The contents of the pattern space can be copied to the hold space, and the contents of the hold space can be copied to the pattern space. A group of commands allows you to move data between the hold space and the pattern space. The hold space is used for temporary storage, and that's it. Individual commands can't address the hold space or alter its contents. The most frequent use of the hold space is to have it retain a duplicate of the current input line while you change the original in the pattern space. [It's also used as a way to do the "move" and "copy" commands that most editors have - but which sed can't do directly because it's designed for editing a stream of input text line-by-line. -GU ] The commands that affect the hold space are:
Each of these commands can take an address that specifies a single line or a range of lines. The hold commands (h ,H ) move data into the hold space and the get commands (g ,G ) move data from the hold space back into the pattern space. The difference between the lowercase and uppercase versions of the same command is that the lowercase command 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 with the contents of the pattern space. The get command replaces the contents of the pattern space with the contents of the hold space. The Hold command puts a newline followed by the contents of the pattern space after the contents of the hold space. (The newline is appended to the hold space even if the hold space is empty.) The Get command puts a newline followed by the contents of the hold space after the contents of the pattern space. The exchange command (x ) swaps the contents of the two buffers. It has no side effects on either buffer. Here's an example to illustrate putting
lines into the hold space and retrieving them later.
We are going to write a script that reads a particular HTML file and
copies all headings to the end of the file for a summary.
The headings we want start with ... <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> The object is to copy those headings into the hold space as sed
reads them.
When sed
reaches the end of the body (at the Look at the script: /^<H[12]>/H /^<\/BODY>/ { i\ <STRONG>Summary:</STRONG> x G s/<\/*H[12]>//g } Any line matching The sequence of x
followed by G
is a way to find a matching
line - in this case, The script could do more cleanup and formatting.
For instance, it could make the saved headings into a list with Here's the result of running the script on the sample file: % For other scripts that use the hold space, see articles 34.17 and 25.12 . For a fanciful analogy that makes clear how it works, see article 34.16 . - , |
|