DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Manipulating text with sed

Hold and get functions

In addition to the pattern space, sed provides a second buffer called the hold space. The contents of the pattern space can be copied to the hold space, then back again. No operations are performed directly on the hold space. sed provides a set of hold and get functions to handle these movements.


h
The h (hold) function copies the contents of the pattern space into a holding area, destroying any previous contents of the holding area. The maximum number of addresses is two.

H
The H function appends the contents of the pattern space to the contents of the holding area. The former and new contents are separated by a newline.

g
The g function copies the contents of the holding area into the pattern space, destroying the previous contents of the pattern space.

G
The G function appends the contents of the holding area to the contents of the pattern space. The former and new contents are separated by a newline. The maximum number of addresses is two.

x
The exchange function interchanges the contents of the pattern space and the holding area. The maximum number of addresses is two.

Suppose you want to search for a phrase in a file, but are not sure whether there is a newline between two words in the phrase. The logical procedure to search for a phrase straddling two lines is the following:
   while (input file exists) {
   	begin:
   	search for phrase in pattern space
   	if found
   		print
   		goto begin
   	else	
   	while (input file exists) {
   		append the next line to the pattern space
   		save a copy of the pattern space
   		discard the first line from the pattern space
   		search for phrase in the pattern space
   		if found
   			print
   		else	
   			restore the saved pattern space
   			strip the newline out of the pattern space
   			search for phrase in the pattern space
   			if found
   				print
   			fi	
   			discard the first line in the pattern space
   		fi	
   		}
   	fi	
   	}
The two crucial requirements for this procedure are that it should use a multiline pattern space, adding and deleting lines from it as the script rolls through the file; and that it must save a copy of the pattern space, make destructive changes to the original, and then retrieve the original copy.

The following is a shell script, not a sed script. It begins by invoking sed: all following commands are enclosed within single quotes. The script accepts two arguments: the string to search for (quoted, if it contains spaces or regular expressions) and the file to search.

   sed '
   /'"$1"'/b
   N
   h
   s/.*\n//
   /'"$1"'/b
   g
   s/ *\n/<Space>/
   /'"$1"'/{
   g
   b
   }
   g
   D' $2
Note that <Space> denotes a literal space character at this point. The first line of sed commands searches for the target phrase; if it is present, sed branches (and goes back to the beginning of its script). See ``Flow-of-control functions'' for details of the b command.

If no match was made, the N function appends the next line to the pattern space; the current pattern space is then temporarily saved in the hold space (with the h function).

sed now removes the first line from the pattern space, then carries out another search for its target string. (If successful, it loops back to the start of the script.) If it still has not found the target string, it copies the saved version of the pattern space back in again and replaces the newline with a space; it searches for the target string again and, if it finds it, prints both lines.

If sed cannot find the target string in its pattern space, it discards the first line in the space and then begins all over again, working from the current line downwards.


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 22 April 2004