Skip to content

Add e substitution flag#413

Open
PranavRJoshi wants to merge 1 commit intouutils:mainfrom
PranavRJoshi:main
Open

Add e substitution flag#413
PranavRJoshi wants to merge 1 commit intouutils:mainfrom
PranavRJoshi:main

Conversation

@PranavRJoshi
Copy link
Copy Markdown

This PR introduces e substitution mentioned in #404.

What this does

Implements the e substitute flag, which executes the result of the substitution as a shell command and replaces the pattern space with stdout (minus trailing newline).

$ echo a | ./target/release/sed  's/.*/echo hi/e'
hi

$ echo a | ./target/release/sed --posix 's/.*/echo hi/e'
sed: <script argument 1>:1:14: error: the 'e' substitute flag is not allowed with --posix or --sandbox

Changes

  • src/sed/command.rs: new execute: bool field on Substitution
  • src/sed/compiler.rs: 'e' arm in compile_subst_flags; rejected at compile time under --posix or --sandbox.
  • src/sed/processor.rs: shell execution via /bin/sh -c after substitution

Tests

  • Three new unit tests for the flag parser covering the intended path, --posix rejection, and --sandbox rejection.
  • Full cargo test run passes.

@sylvestre
Copy link
Copy Markdown
Contributor

please run rustfmt

Comment thread src/sed/processor.rs Outdated
// Execute the pattern space as a shell command if the 'e' flag is set
if sub.execute {
let cmd_str = pattern.as_str()?.to_string();
let output_bytes = std::process::Command::new("/bin/sh")
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.

/bin/sh ?
it won't work on windows

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Should I make it conditionally compile on UNIX-like system only and warn out the insufficient capabilities on Windows, or do I instead execute the cmd.exe for Windows? The latter one would still require conditional compilation, so i can add #[cfg] for UNIX-like and Windows.

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.

nope, it is a cross platform project. this should be a tier-1 feature on Windows too

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

❌ Patch coverage is 63.23529% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.72%. Comparing base (f783eb9) to head (9b1559f).

Files with missing lines Patch % Lines
src/sed/processor.rs 0.00% 25 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #413      +/-   ##
==========================================
+ Coverage   82.20%   82.72%   +0.51%     
==========================================
  Files          13       13              
  Lines        5542     5545       +3     
  Branches      310      307       -3     
==========================================
+ Hits         4556     4587      +31     
+ Misses        983      955      -28     
  Partials        3        3              
Flag Coverage Δ
macos_latest ?
ubuntu_latest 82.72% <63.23%> (-0.28%) ⬇️
windows_latest ?

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

☔ View full report in Codecov by Sentry.
📢 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
Copy link
Copy Markdown
Contributor

please also add tests in tests/by-util/test_sed.rs

@PranavRJoshi
Copy link
Copy Markdown
Author

Thanks for the guidance, will work into it.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 6, 2026

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing PranavRJoshi:main (7c2e7fd) with main (f783eb9)

Open in CodSpeed

@PranavRJoshi PranavRJoshi changed the title Feat: Introduce e subsitution Feat: Introduce e substitution May 6, 2026
@PranavRJoshi PranavRJoshi changed the title Feat: Introduce e substitution Add e substitution flag May 6, 2026
@PranavRJoshi PranavRJoshi reopened this May 6, 2026
Introduce the 'e' flag for the substitution command, which pipes the
pattern space through a shell and replaces it with the output.

- Add shell_command() with platform-specific implementations (sh on
  Unix, cmd on Windows)
- Handle WASM gracefully with a fallback
- Trim trailing newlines accounting for Windows line endings
- Add tests for the new flag, with conditional compilation for
  platform-specific behavior

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>
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