$ cat rgb
lower (#1): "red green blue"
UPPER (#2): "RED GREEN BLUE"
$ sed '/#/ {s//=/; :k n; bk}' rgb
lower (=1): "red green blue"
UPPER (#2): "RED GREEN BLUE"
I know that sed
exits without autoprint if there is not any next line to append. However in this example sed
printed the last line. How does it work?
The
-n
option disables autoprint.You replaced
#
with=
in the first line with#
(the pattern space). The pattern space then got printed byn
because there was no-n
option. Then the pattern space was replaced with the next line, then we looped by ton
, which caused the pattern space to be printed because autoprint was not disabled, and then there were no more lines of input sosed
exited.Maybe it's clearer if you use
-n
and add ap
to your loop:after
n
,p
forces printing of all but the first line.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 thep
command in different positions in the loop.If you only want to print one line after changing it, you could use
q
: