I noticed the following interesting behavior:
$ printf '%s\n' line{1..2} | sed $'1a\\\nPREFIX'
line1
PREFIXline2
$
Interestingly, this behavior is only possible for the last command in a Sed script, as to put another command afterwards requires a newline.
It also works with the insert command:
$ printf '%s\n' line{1..2} | sed $'1i\\\nPREFIX'
PREFIXline1
line2
$
Can this behavior be depended upon?
I don't see it mentioned one way or another in the POSIX specs for Sed. It just says that the text
may consist of one or more lines. (If a line doesn't end in a newline character, is it still a line?)
It also works with multiple line insertions:
$ printf '%s\n' line{1..2} | sed $'1a\\\n ****/\n1i\\\n/****\\\n * '
/****
* line1
****/
line2
$
a
/i
command, so it should not be treated different from the insertion at the end of the script. Whether or not the newline is to be output at all, is ambiguous in the standard, as I see it, so theoretically yoursed
could insert it or not, but should to it independent from whether this is the last command. – Philippos Sep 7 '17 at 6:21sed -e x
is the same assed -f file
wherefile
containsx\n
. Actually, you'll find thatsed -e script
works OK, onlysed script
has the issue confirming even more clearly that it is a bug. – Stéphane Chazelas Sep 7 '17 at 6:36