Subject: GNU sed in POSIX mode sometimes not matching newlines

From: Wolfgang Jeltsch <[email-masked]>
Date: Sun, 13 Apr 2025 23:41:30 +0300

Hi!

I’m using GNU sed 4.9 under Ubuntu 24.04.2, and I think I’ve found a bug
in it.

Consider the following shell command:

    echo 'A
    B
    C' | sed -E 'N; N; s/^A\n[[:alpha:]]\n*/XXX/'

Executing this results in the following output:

    XXXC

This is what I would expect.

However, when I run the same command but with GNU sed in POSIX
compatibility mode, by either setting POSIXLY_CORRECT to a non-empty
value or inserting the argument `--posix` into the `sed` invocation,
then I get the following output:

    XXX
    C

I cannot see a reason for this to be considered a correct result, and
I’m quite sure that it violates the POSIX standard.

What’s more, that difference between GNU and POSIX mode does not show up
with slightly simplified examples. Consider the following commands:

    echo 'B
    C' | sed -E 'N; s/^[[:alpha:]]\n*/XXX/'

    echo 'A
    B
    C' | sed -E 'N; N; s/^A\nB\n*/XXX/'

Both result in the following output, no matter whether POSIX
compatibility is requested or not:

    XXXC

This is what I’m considering the correct result.

All the best,
Wolfgang