Skip to content

Add z command (clear pattern space) #396

@sylvestre

Description

@sylvestre

Bug

The GNU z command — clear the pattern space (sets it to empty
without printing) — is not implemented.

Reproduction

$ printf "a\nb\n" | /usr/bin/sed 'z'
                 # two empty lines

$ printf "a\nb\n" | ./target/release/sed 'z'
sed: <script argument 1>:1:1: error: invalid command code `z'

What it should do

From the GNU manual:

z This command empties the contents of the pattern space.

Equivalent to s/.*// but never sets the substitution flag (so does
not affect t/T). Takes 0 or 1 address; rejected under --posix.

Suspected place to add it

src/sed/compiler.rs:1276get_cmd_spec:

'z' => Ok(CommandSpec {
    n_addr: 2,
    handler: compile_empty_command,
}),

…and in processor.rs add an execution case that does
pattern_space.clear() (or equivalent for the type used).

Note: not to be confused with the -z / --null-data CLI flag, which
is already implemented at mod.rs:106.

Affected GNU testsuite tests

Direct test (z.sh) is not in the runner's set, but compile-errors
asserts that z is rejected under --posix. Once z exists,
bad_command rejection works automatically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions