Wednesday, October 26, 2022

[SOLVED] What is \n\nnd supposed to do?

Issue

echo n | sed '\n\nnd'

This command prints n with GNU sed. With BSD sed, it doesn't print anything.

The href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html#tag_20_116_13_02" rel="nofollow noreferrer">POSIX sed spec. says:

  • In a context address, the construction \cBREc, where c is any character other than <backslash> or <newline>, shall be identical to /BRE/. If the character designated by c appears following a <backslash>, then it shall be considered to be that literal character, which shall not terminate the BRE. For example, in the context address \xabc\xdefx, the second x stands for itself, so that the BRE is abcxdef.

  • The escape sequence \n shall match a <newline> embedded in the pattern space. A literal <newline> shall not be used in the BRE of a context address or in the substitute function.

but doesn't elaborate any further on these contradictory statements.

So my question is, which behavior is correct? Or is it intentionally left unspecified?


Solution

There's an update; with this commit GNU sed no longer prints n for the command in OP.


According to a reply to my email on Austin Group mailing list (quoted below), the standard is unclear on this, and both behaviors are correct. HP-UX and Solaris adopted the GNU behavior too; so it's not a bug in implementations, but a lack of clarity in the standard.

Neither is more correct than the other because, as you said yourself, the standard is unclear. A formal interpretation would say "The standard is unclear on this issue, and no conformance distinction can be made between alternative implementations based on this."

Given that implementations differ, we should probably make the behaviour explicitly unspecified.



Answered By - oguz ismail
Answer Checked By - Marie Seifert (WPSolving Admin)