diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index bcfff31..8ef5bc3 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -1327,6 +1327,10 @@ fn get_cmd_spec( n_addr: 2, handler: compile_empty_command, }), + 'z' if !posix => Ok(CommandSpec { + n_addr: 2, + handler: compile_empty_command, + }), 'l' => Ok(CommandSpec { n_addr: 2, handler: compile_number_command, diff --git a/src/sed/processor.rs b/src/sed/processor.rs index 45d6b26..bc42b8a 100644 --- a/src/sed/processor.rs +++ b/src/sed/processor.rs @@ -621,6 +621,12 @@ fn process_file( let trans = extract_variant!(command, Transliteration); transliterate(&mut pattern, trans)?; } + 'z' => { + // Clear the pattern contents, but preserve newline state + // so automatic printing still emits an empty record. + let (pat_content, _) = pattern.fields_mut()?; + pat_content.clear(); + } ':' => { // Branch target; do nothing. } diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index b31fed7..ee47e7e 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -358,6 +358,42 @@ check_output!( ["-e", r"y10\123456789198765432\101", LINES1] ); check_output!(trans_no_new_line, ["-e", r"y/l/L/", NO_NEW_LINE]); + +#[test] +fn pattern_clear_with_z_command() { + new_ucmd!() + .arg("z") + .pipe_in("a\nb\n") + .succeeds() + .stdout_is("\n\n"); +} + +#[test] +fn pattern_clear_with_z_command_silent_print() { + new_ucmd!() + .args(&["-n", "z;p"]) + .pipe_in("a\nb\n") + .succeeds() + .stdout_is("\n\n"); +} + +#[test] +fn pattern_clear_with_z_preserves_substitution_flag() { + new_ucmd!() + .args(&["-n", "s/a/b/;z;t changed;b;:changed;c\\\nchanged"]) + .pipe_in("a\n") + .succeeds() + .stdout_is("changed\n"); +} + +#[test] +fn pattern_clear_with_z_is_non_posix() { + new_ucmd!() + .args(&["--posix", "z"]) + .fails() + .code_is(1) + .stderr_is("sed: