'n'
(next) If auto-print is not disabled, print the pattern space,
then, regardless, replace the pattern space with the next line of
input. If there is no more input then 'sed' exits without
processing any more commands.
The -n
option disables autoprint.
You replaced #
with =
in the first line with #
(the pattern space). The pattern space then got printed by n
because there was no -n
option. Then the pattern space was replaced with the next line, then we looped by to n
, which caused the pattern space to be printed because autoprint was not disabled, and then there were no more lines of input so sed
exited.
Maybe it's clearer if you use -n
and add a p
to your loop:
$ sed -n '/#/ {s//=/; :k n;p; bk}' rgb
UPPER (#2): "RED GREEN BLUE"
after n
, p
forces printing of all but the first line.
$ sed -n '/#/ {s//=/; :k p;n; bk}' rgb
lower (=1): "red green blue"
UPPER (#2): "RED GREEN BLUE"
before n
, p
forces the pattern space to be printed every time. n
never prints anything, because autoprint is disabled.
Try removing the -n
option with the p
command in different positions in the loop.
If you only want to print one line after changing it, you could use q
:
$ sed '/#/ s//=/;q' rgb
lower (=1): "red green blue"