Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Concept of 'Hold space' and 'Pattern space' in sed

Tags:

linux

sed

I'm confused by the two concepts in sed: hold space and pattern space. Can someone help explain them?

Here's a snippet of the manual:

h H    Copy/append pattern space to hold space. g G    Copy/append hold space to pattern space.  n N    Read/append the next line of input into the pattern space. 

These six commands really confuse me.

like image 676
ChenQi Avatar asked Oct 11 '12 06:10

ChenQi


People also ask

What is pattern space and hold space in sed?

Pattern space is the internal sed buffer where sed places, and modifies, the line it reads from the input file. Hold space: This is an additional buffer available where sed can hold temporary data.

What is pattern space?

A pattern space is a graphical representation of a set of input patterns and their desired responses (Dawson, 2004). Each input pattern is represented as a point in this space. The coordinates of each pattern are obtained by treating each input unit's activity as a coordinate value.

What are sed commands?

The SED command in Linux stands for Stream EDitor and is helpful for a myriad of frequently needed operations in text files and streams. Sed helps in operations like selecting the text, substituting text, modifying an original file, adding lines to text, or deleting lines from the text.

What is the best sed?

The sed rate test reports in millimeters (mm) the distance between the clear liquid (plasma) at the top of the tube and your red blood cells after 1 hour. The normal range is: 0 to 15 mm/hour in men younger than 50. 0 to 20 mm/hour in men older than 50.


1 Answers

When sed reads a file line by line, the line that has been currently read is inserted into the pattern buffer (pattern space). Pattern buffer is like the temporary buffer, the scratchpad where the current information is stored. When you tell sed to print, it prints the pattern buffer.

Hold buffer / hold space is like a long-term storage, such that you can catch something, store it and reuse it later when sed is processing another line. You do not directly process the hold space, instead, you need to copy it or append to the pattern space if you want to do something with it. For example, the print command p prints the pattern space only. Likewise, s operates on the pattern space.

Here is an example:

sed -n '1!G;h;$p' 

(the -n option suppresses automatic printing of lines)

There are three commands here: 1!G, h and $p. 1!G has an address, 1 (first line), but the ! means that the command will be executed everywhere but on the first line. $p on the other hand will only be executed on the last line. So what happens is this:

  1. first line is read and inserted automatically into the pattern space
  2. on the first line, first command is not executed; h copies the first line into the hold space.
  3. now the second line replaces whatever was in the pattern space
  4. on the second line, first we execute G, appending the contents of the hold buffer to the pattern buffer, separating it by a newline. The pattern space now contains the second line, a newline, and the first line.
  5. Then, h command inserts the concatenated contents of the pattern buffer into the hold space, which now holds the reversed lines two and one.
  6. We proceed to line number three -- go to the point (3) above.

Finally, after the last line has been read and the hold space (containing all the previous lines in a reverse order) have been appended to the pattern space, pattern space is printed with p. As you have guessed, the above does exactly what the tac command does -- prints the file in reverse.

like image 185
January Avatar answered Oct 03 '22 00:10

January