pattern space and hold space of sed

Copied from: stackoverflow

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:

first line is read and inserted automatically into the pattern space on the first line, first command is not executed; h copies the first line into the hold space. now the second line replaces whatever was in the pattern space 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. Then, h command inserts the concatenated contents of the pattern buffer into the hold space, which now holds the reversed lines two and one. 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, p 大专栏  pattern space and hold space of sedattern space is printed with p. As you have guessed, the above does exactly what the tac command does – prints the file in reverse.

- translation -
when sed read from the file line by line, the current line is read that will be inserted into the pattern buffer (pattern space). pattern buffer is like a temporary buffer, intermediate result register is to present information to the first temporary storage inside. When you notice sed to print, it is printing pattern buffer.

Hold buffer / hold space is more like a long-register, you can capture information storage and reuse them over and then when sed in dealing with the other lines. You can not directly manipulate hold space, however, if you want to use it on anything when you can copy it and put it appended to the pattern space. For example, the print command p just print out the pattern space. Same, s command operates only on the pattern space.

Let's look at the actual operation: sed -n '1 G; h; $ p!'

(-N option is to cancel the automatic print)

There are three commands:!. 1 G, h and $ p 1 G has an address, 1 (first row), but means that the command can be executed anywhere in the first line!!. $ P contrast will perform on the last line. The actual process is as follows:

第一行被读取并自动插入到了 pattern space
在第一行,第一条命令并没有被执行;h 将第一行拷贝到 hold space。
现在第二行取代了 pattern space 中的内容
在第二行,我们首先执行 G,追加 hold buffer 中的内容到 pattern buffer,用一个换行符将其分割。pattern space 中现在包含第二行,一个换行符,还有第一行。
接下来,h 命令将已经连接在一起的 pattern buffer 插入到 hold space,那里现在保存的是倒序的第二行和第一行。
之后我们继续处理第三行 -- 重复上面的第三步。

Eventually, after already being read in the last row and hold space (contains all reverse line) has been appended to the pattern space, pattern space is printed p. As you might expect, it is something more than tac command does - print files in reverse order.

Guess you like

Origin www.cnblogs.com/lijianming180/p/12302290.html