| 
 |  | 
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.
   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.