Skip to content

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

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

sylvestre wants to merge 0 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

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 95.10%. Comparing base (273dcfb) to head (2031175).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #610   +/-   ##
=======================================
  Coverage   95.10%   95.10%           
=======================================
  Files          14       14           
  Lines        7490     7499    +9     
  Branches      443      443           
=======================================
+ Hits         7123     7132    +9     
  Misses        366      366           
  Partials        1        1           
Flag Coverage Δ
macos_latest 96.59% <100.00%> (+<0.01%) ⬆️
ubuntu_latest 96.53% <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.

@sylvestre
sylvestre force-pushed the sed-subst-number-and-g branch from d1a8efd to 2031175 Compare November 7, 2026 14:59
Copilot AI balanced review requested due to automatic review settings November 7, 2026 14:59

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.

@sylvestre sylvestre closed this Nov 9, 2026
@sylvestre
sylvestre force-pushed the sed-subst-number-and-g branch from 2031175 to 5e2821e Compare November 9, 2026 13:27
Copilot AI balanced review requested due to automatic review settings November 9, 2026 13:27

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 wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

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