Skip to content

sed: print the c text when a numeric, relative or step range ends - #617

Open
jayhemnani9910 wants to merge 2 commits into
uutils:mainfrom
jayhemnani9910:change-at-range-end
Open

jayhemnani9910 wants to merge 2 commits into
uutils:mainfrom
jayhemnani9910:change-at-range-end

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

With a range whose end is a line number, +N or ~N, c deletes the lines but never prints its text. GNU sed 4.9 vs main:

$ seq 6 | sed '2,4c\X'
GNU:  1 X 5 6
main: 1 5 6

The same for 2,+2c and 2,~4c; /re/,/re/c works. Related cases in the same code:

$ seq 6 | sed '2,4!c\X'             GNU: X 2 3 4 X X   main: 2 3 4 X
$ seq 3 | sed '2,5c\X'              GNU: 1             main: 1 X
$ seq 9 | sed '/2/,/3/c\X
6,8c\Y'                              GNU: 1 X 4 5 Y 9   main: 1 X 4 5 Y Y Y 9
$ printf 'a\na\na\na\nb\n' | sed '/a/,3d'   GNU: b     main: a b

applies only set last_address when a regex or $ ended the range, and never cleared it, so c either missed the end or printed on every later line; c also printed at the last input line of an unfinished range and skipped negated ranges. Now last_address is cleared for each command and set when a line, +N or ~N end is reached; the range closes on that line, so the next line can start it again (the /a/,3d case). c prints at the end of a range, and on every line of a negated one.

New tests test_change_command_prints_text_at_range_end and test_range_with_line_end_restarts_on_next_line fail on main. cargo test: 748 passed (746 on main plus the two), clippy and fmt clean. 26 scripts with c, d, p, n, N and blocks over numeric, relative, step and regex ranges give the same output as GNU sed 4.9 on three inputs.

Copilot AI balanced review requested due to automatic review settings October 24, 2026 07:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.37%. Comparing base (caef481) to head (1937eb2).
⚠️ Report is 30 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #617      +/-   ##
==========================================
+ Coverage   94.93%   95.37%   +0.43%     
==========================================
  Files          14       14              
  Lines        7292     7675     +383     
  Branches      434      459      +25     
==========================================
+ Hits         6923     7320     +397     
+ Misses        368      354      -14     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.83% <100.00%> (+0.37%) ⬆️
ubuntu_latest 96.77% <100.00%> (+0.36%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/sed/processor.rs
Ok(false)
} else {
if linenum - start == *n {
command.start_line = None;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also changes the restart for +N, e.g. printf 'a\na\na\na\n' | sed '/a/,+1d'
please add a test for it too

Comment thread src/sed/processor.rs
// when the range is negated, then start the next cycle.
pattern.clear();
if command.addr2.is_none() || context.last_address || reader.last_line()? {
if command.addr2.is_none() || command.non_select || context.last_address {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropping reader.last_line() changes the output for a range still open at EOF, please run the GNU testsuite to make sure nothing regresses:
git clone https://github.com/mirror/sed.git ../gnu.sed
./util/run-gnu-testsuite.sh

Comment thread src/sed/processor.rs
@@ -111,6 +121,7 @@ fn applies(
// Inclusive end on multiple of step
if linenum.is_multiple_of(*step) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about 4,~4c\X? i think GNU matches only line 4 when addr1 is already a multiple of N, here the range would go on until 8
could you please check and add it to the test?

@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

processor.rs:100 (+N restart): yes, that is the same restart. Added /a/,+1d and /a/,+1c\X on a a a a b to test_range_with_line_end_restarts_on_next_line in 1937eb2: GNU sed 4.9 prints b and X X b. Before this PR, /a/,+1d printed a.

processor.rs:729 (GNU testsuite): ran ./util/run-gnu-testsuite.sh against a fresh clone of mirror/sed, on the PR base (caef481) and on this branch: both give 65 total, 21 passed, 34 failed, 10 skipped, and the same tests fail. A c range still open at EOF prints nothing, as GNU does (seq 6 | sed '5,8c\X' prints 1-4, already in the test).

processor.rs:122 (4,~4c\X): GNU sed 4.9 doesn't stop at line 4 there. seq 10 | sed -n '4,~4p' prints 4 to 8, and seq 10 | sed '4,~4c\X' prints 1 2 3 X 9 10. The branch does the same. I added 4,~4c\X to the test on 6 lines: the range is still open at EOF, so GNU prints only 1 2 3, which a range that ended on line 4 would not.

The new cases fail on the PR base and pass here. cargo test, cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean.

Copilot AI balanced review requested due to automatic review settings November 11, 2026 07:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Nov 11, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 2.81%

2 warnings

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 10 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ no_op_short 2.2 s 2.1 s +2.81%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing jayhemnani9910:change-at-range-end (1937eb2) with main (cab1c6e)

Open in CodSpeed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants