Skip to content

sed: accept a number combined with the g substitute flag - #610

Open
sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:sed-subst-number-and-g
Open

sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:sed-subst-number-and-g

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor

s/x/y/2g (replace the 2nd match and every later one) and s/x/y/g2 are valid in GNU sed but were rejected with "multiple 'g' or numeric flags in substitute command", and unit tests asserted that. g is now tracked separately from the occurrence number. Repeating either one is still an error.

$ echo aaaa | sed 's/a/b/2g'
abbb

`s/x/y/2g` (replace the 2nd match and every later one) and `s/x/y/g2`
are valid in GNU sed, but were rejected with "multiple 'g' or numeric
flags". Track `g` separately from the occurrence number so the two can
be combined; repeating either one is still an error.

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.

🟢 Approval recommended

The parser and execution changes are consistent, regression tests cover the new behavior, and no blocking issues remain.

No open issues 🎉

What changed in this PR

Adds GNU-compatible support for combining a numeric occurrence with sed’s g substitution flag.

Changes:

  • Tracks global replacement separately from the occurrence number.
  • Replaces the selected match and every later match when combined.
  • Tests both flag orders and preserves duplicate-flag rejection.
File Description
tests/​by-util/​test_sed.rs Adds combined-flag and capture replacement tests.
src/​sed/​processor.rs Applies global replacement from the selected occurrence.
src/​sed/​compiler.rs Parses numeric and global flags independently; updates tests.
src/​sed/​command.rs Adds separate global substitution state.

Review effort: Balanced

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.86%. Comparing base (b1ea0dc) to head (d1a8efd).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #610   +/-   ##
=======================================
  Coverage   94.85%   94.86%           
=======================================
  Files          14       14           
  Lines        7230     7239    +9     
  Branches      432      432           
=======================================
+ Hits         6858     6867    +9     
  Misses        371      371           
  Partials        1        1           
Flag Coverage Δ
macos_latest 96.39% <100.00%> (+<0.01%) ⬆️
ubuntu_latest 96.33% <100.00%> (+<0.01%) ⬆️
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.

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.

2 participants