The sed's document states
P
Print up to the first embedded newline of the current pattern space.
I did some experiment and trying to understand it. Below is my testing.
root:[~]# seq 3 | sed -n '/2/{iline1\nline2;P}'
sed: -e expression #1, char 0: unmatched `{'
root:[~]#
Here -n
disables the auto-printing of the pattern space. /2/
matches the second line, which has the content 2
. So when the second line has been matched, the pattern space has the content 2
. The i
command inserts line1\nline2
and a newline character to the beginning of the pattern space. So the pattern space should have the content line1\nline2\n2
. Now the P
command print up to the first new line, so line1
should be printed and should be the result of the whole command. Is my understanding correct? Why the error? Thanks in advance.