Subject: Re: bug#27354: strange behavior of sed

From: Assaf Gordon <[email-masked]>
Date: Wed, 21 Jun 2017 20:18:59 +0000

Hello,

On Tue, Jun 20, 2017 at 02:24:51PM +0800, Han Lu wrote:
>But if I put 'i' command before 'd' command, shouldn't the input new lines
>be deleted ? Since new inputed lines contain the pattern 'd' command want.

There is a subtlely in the 'i' (insert) command:
The added content is not stored in the pattern space and is
not subject to later manipulations (such as pattern matching, 's///'
and 'd').

The GNU sed manual says:

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

Observe the following:

    $ seq 3 | sed -e 1iX -e 's/X/Y/'
    X
    1
    2
    3

And similarly with match+delete command:

    $ seq 3 | sed -e 1iX -e '/X/d'
    X
    1
    2
    3

Similar subtlety also applies to 'a' (append) and 'c' (change) commands.

The manual should probably be improved to explicitly mention
these points (added to my mental 'todo' list).

regards,
 - assaf