Subject: bug#47390: -i and -n options are order-sensitive

From: Nora Platiel <[email-masked]>
Date: Wed, 28 Apr 2021 23:09:31 +0200

Hello,
they are not order-sensitive, but the -i flag takes an optional argument (SUFFIX), therefore it cannot be grouped together with other short flags unless it is the last one.

> To remove the first N lines from a big text file I used to do:
>
> sed -in '1,Nd' bigfile.txt
>
> and it was working as expected.

In this invocation, the "n" is not a flag, it is the argument to the -i flag.
Same as:
sed --in-place=n '1,Nd' bigfile.txt
which tells sed to use "n" as the backup suffix (creating "bigfile.txtn" as backup of "bigfile.txt").

> Assuming the order of the flags does not matter I did once:
>
> sed -ni '1,Nd' bigfile.txt
>
> which deleted the whole content of the file. Is this behaviour expected?

In this invocation, you are passing both the -n and -i flags (the latter with no argument, which disables backup).
Same as:
sed --quiet --in-place '1,Nd' bigfile.txt
The output file will be empty as expected, because -n/--quiet disables automatic printing and you are not using any printing command in your sed script.

> From the user point of view this seems like a rather dangerous bug.

The -i flag of sed is well documented, and the docs include a warning for the very same pitfall you encountered:
https://www.gnu.org/software/sed/manual/sed.html#Command_002dLine-Options
You may think that flags with optional arguments are error prone (and I would agree), but there's nothing we can do about it here. GNU flags have worked like this for a long time.

> I think, flags not taking an argument should be insensitive to the order.

Usually they are, unless they are exclusive and the last one takes effect, but this is not the problem here.

> Currently, to get the same result without running the risk to forget the
> right order of the flags and deleting the file content, I do:
>
> sed -i -e '1,Nd' bigfile.txt

This has yet a different effect: the -i flag is without argument (no backup) and the -n is not present (autoprinting is on). The -e makes no difference in this case because there is only one script. If you want the "-in" effect, the only alternative is "--in-place=n".

Regards,
NP