这里只是一个记录, 把Stack Overflow一个大神的讲解抄过来,原地址请戳这里
pattern space
当sed
一行一行的读取文件, 正在读取的当前行会被插入到pattern buffer
(pattern space
)中,pattern buffer像是一个临时的缓冲区, 即存储当前信息的暂存器。当你让sed
打印的时候, 他就将pattern buffer的内容打印出来了。
hold space
Hold buffer / hold space
像一个长期储存, 你可以将你获取的一些东西储存到其中然后待会在sed
处理别的行的时候再取出来用,你不能直接对hold space
进行操作, 取而代之的是将他的内容复制或者添加到pattern space
再操作他, 举个例子,打印命令p
只打印pattern space
的内容, 同样s
操作也是只对于pattern space
操作;
一个例子
echo -e '1\n2\n3' | sed -n '1!G;h;$p'
# 输出:
3
2
1
(-n
参数可以阻止默认打印所有行的行为)
这里有3个命令:1!G
, h
和$p
, 1!G
有一个地址,1
(代表第一行),但是!
代表取反, 这个命令也就是说除了第一行, 其他所有行都要执行G
,$p
表示只有在最后一行才打印,因此这里真实发生的是:
- 第一行被读取并自动插入到
pattern space
- 在第一行, 第一个命令
G
没有执行;h
命令复制第一行到hold space
中;- 现在第二行替换掉了
pattern space
中的内容,无论里面有什么- 在第二行, 首先我们执行
G
,将hold buffer
中的内容添加到pattern buffer
中;- 以行分隔符分割, 现在
pattern space
包含了第二行,换行,还有第一行- 然后,
h
命令将pattern buffer
内级联的内容插入到hold space
(现在hold space
保存有反转的行2 行1)- 然后我们处理第3行, 从第3步开始……
最终, 当最后一行被读取还有hold space
(包含之前的所有行,按照反转的顺序)已经添加到pattern space
, pattern space
被p
命令打印, 就如你已经猜到的一样, 上面所做的一切其实就是tac
命令的实现--反转地打印文件.
原文如下:
pattern space
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 space
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 executeG
, appending the contents of the hold buffer to the pattern buffer,
5.separating it by a newline. The pattern space now contains the second line, a newline, and the first line.
6.Then,h
command inserts the concatenated contents of the pattern buffer into the hold space, which now holds the reversed lines two and one.
7.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.