From 7be3e25bc0043e85047221b427879c4c075a951d Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:26:00 +0530 Subject: [PATCH 1/2] sed: accept first~step as the start of an address range GNU sed starts a range on every line first~step selects, so '1~3,+1p' prints lines 1, 2, 4, 5, ... Until now the ',' after the step was rejected as an unknown command. --- src/sed/command.rs | 1 + src/sed/compiler.rs | 15 ++++++++++++++- src/sed/processor.rs | 9 +++++++++ tests/by-util/test_sed.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/sed/command.rs b/src/sed/command.rs index e652e97e..45978365 100644 --- a/src/sed/command.rs +++ b/src/sed/command.rs @@ -114,6 +114,7 @@ pub enum Address { RelLine(usize), // Relative line Last, // Last line StepMatch(usize), // Lines matching specified step from first + Step(usize, usize), // first~step starting a range: first, first+step, ... StepEnd(usize), // Range ending at specified step from first } diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index c4534606..f9424b5c 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -357,7 +357,7 @@ fn compile_address_range( } line.eat_spaces(); - if n_addr == 1 && !line.eol() && matches!(line.current(), ',' | '~') { + while n_addr == 1 && !line.eol() && matches!(line.current(), ',' | '~') { let is_step_match = line.current() == '~'; // E.g. 0~2: Pick even-numbered lines line.advance(); line.eat_spaces(); @@ -403,6 +403,19 @@ fn compile_address_range( 0 // dummy, not used }; + // first~step,addr2: the lines first~step selects start the range. + line.eat_spaces(); + if is_step_match + && !line.eol() + && line.current() == ',' + && let Some(Address::Line(first)) = cmd.addr1 + && (first, step_n) != (0, 0) + { + cmd.addr1 = Some(Address::Step(first, step_n)); + is_line0 = false; + continue; + } + // Address 0 needs a regex end or a positive ~step (0~0 selects no line). if is_line0 && !matches!(addr2, Address::Re(_)) && !(is_step_match && step_n > 0) { return compilation_error(lines, line, ERR_ADDRESS_0_USAGE); diff --git a/src/sed/processor.rs b/src/sed/processor.rs index b6831169..7777b8d1 100644 --- a/src/sed/processor.rs +++ b/src/sed/processor.rs @@ -59,6 +59,15 @@ fn match_address( Address::Line(lineno) => Ok(context.line_number == *lineno), + Address::Step(first, step) => { + let n = context.line_number; + Ok(if *step == 0 { + n == *first + } else { + n >= *first && (n - first).is_multiple_of(*step) + }) + } + // Recognize "$" as the last line of last file. This is consistent // with the original 7th Research Edition implementation: // https://github.com/dspinellis/unix-history-repo/blob/Research-V7/usr/src/cmd/sed/sed1.c#L665 diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index a4e7450f..3b5b8fcd 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -3789,6 +3789,35 @@ fn test_step_without_number_is_zero(#[values("3~=", "3,~=", "3,~0=")] script: &s .stdout_is("3\n"); } +#[test] +fn test_step_match_starts_range() { + // `first~step,addr2` starts the range on every line first~step selects. + let input = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n"; + for (script, expected) in [ + ("1~3,+1p", "1\n2\n4\n5\n7\n8\n10\n11\n"), + ("1~3,5p", "1\n2\n3\n4\n5\n7\n10\n"), + ("0~4,~3p", "4\n5\n6\n8\n9\n12\n"), + ("2~3,/7/p", "2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n"), + ("1~0,3p", "1\n2\n3\n"), + ("3~2,1p", "3\n5\n7\n9\n11\n"), + ] { + new_ucmd!() + .args(&["-n", script]) + .pipe_in(input) + .succeeds() + .stdout_is(expected); + } +} + +#[test] +fn test_step_match_zero_zero_range_start() { + new_ucmd!() + .args(&["-n", "0~0,3p"]) + .fails() + .code_is(1) + .stderr_is("sed: