0

Below is an excerpt from Gnu sed's documentation at https://www.gnu.org/software/sed/manual/sed.html

i text
insert text before a line. This is a GNU extension to the
standard i command - see below for details.

i\
text
Immediately output the lines of text which follow this command.

The wording of these two syntaxes is quite different. In the past, I thought they have the exact same behavior and it's just the syntax is different. I did a bunch of experiment and all tests give the same result. I want to confirm, are there any differences between these two syntaxes? Thanks in advance.

1
  • They work the same, only the syntax is different. Including the range GNU extension (eg. 3,4ifoo) works with both syntaxes. You can have a look at the source code,
    – user313992
    Aug 27, 2019 at 22:16

1 Answer 1

2

The i text syntax is extension to the POSIX standard syntax for the i command in sed, provided as a convenience (as often is the case with GNU extension).

The standard i command looks like

i\
text

and POSIX documents this tersely with

Write text to standard output.

GNU sed provides the same syntactical convenience for the a and c commands as it does for i. The following is from the info sed documentation regarding the a command (GNU sed version 4.2.2):

As a GNU extension, if between the a and the newline there is other than a whitespace-\ sequence, then the text of this line, starting at the first non-whitespace character after the a, is taken as the first line of the TEXT block. (This enables a simplification in scripting a one-line add.) This extension also works with the i and c commands.

This implies that the two commands

i text

and

i\
text

are identical in GNU sed.

2
  • they've changed the wording (to the one from the OP) in newer versions of the info manual.
    – user313992
    Aug 27, 2019 at 22:16
  • 1
    @mosvy I noticed, which is why I included the version I have on my system. The old wording that I quoted was clearer and more precise IMHO.
    – Kusalananda
    Aug 27, 2019 at 22:17

Not the answer you're looking for? Browse other questions tagged .