From the documentation of sed:
sed maintains two data buffers: the active pattern space, and the
auxiliary hold space. Both are initially empty.
I initially think the value of pattern space and hold space is null (nothing). But from the following example, it seems that the initially value of them is a single newline character (\n).
[root@localhost ~]# cat e.txt
aa
bb
cc
dd
[root@localhost ~]# cat e.txt | sed -r '/c/{x;p;x}'
aa
bb
cc
dd
[root@localhost ~]#
Is my understanding right?
Thanks.
I think the answer is that the
pcommand, like the default print action, is actually adding a newline to the end of the empty pattern space. This is based on this little snippet from the GNUseddocumentation (just below that bit you quote, by the way):In other words, the line being held in the pattern (and hold) space does not have the trailing newline – the
aaline is held asaarather thanaa<newline>.Of course, the hold space may still contain multiple lines but that just means that executing the
Hcommand on the first two lines of your file will give you a hold space containingaa<newline>bb, notaa<newline>bb<newline>.