Skip to content

n starts a new cycle instead of continuing with the next command #568

Description

@SichenLiang

After reading the next input line, uutils sed's n command ends the current cycle. The new line is processed from the beginning of the script instead of continuing with the command after n.

GNU sed continues with the next command.

This was reproduced with GNU sed 4.10 and uutils sed built from cffecd15, both under LC_ALL=C.

For example, n;d should keep every other line:

$ printf 'a\nb\nc\nd\n' | sed 'n;d'
a
c

$ printf 'a\nb\nc\nd\n' | ./target/release/sed 'n;d'
a
b
c
d

The example used in the GNU sed manual shows the same difference:

$ seq 6 | sed 'n;n;s/./x/'
1
2
x
4
5
x

$ seq 6 | ./target/release/sed 'n;n;s/./x/'
1
2
3
4
5
6

POSIX describes n as: "Write the pattern space to standard output if the default output has not been suppressed, and replace the pattern space with the next line of input, less its terminating <newline>. If no next line of input is available, the n command verb shall branch to the end of the script and quit without starting a new cycle."

The n branch in src/sed/processor.rs currently uses break, which ends the cycle. The adjacent N implementation already preserves the next command in context.input_action and resumes there after reading another line. The same mechanism can be used for n, replacing the pattern space instead of appending to it.

I have a patch for this. Because the control-flow change also exposes the existing #541, I wanted to confirm the approach before opening a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions