Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: ability to exit after one replacement #105

Closed
ngirard opened this issue Oct 23, 2020 · 5 comments · Fixed by #267
Closed

Feature request: ability to exit after one replacement #105

ngirard opened this issue Oct 23, 2020 · 5 comments · Fixed by #267
Labels
C-enhancement Category: New feature or request

Comments

@ngirard
Copy link

ngirard commented Oct 23, 2020

I ran against this situation multiple times: I only need to search & replace the first occurrence of a pattern.

It'd be useful if sd allowed to exit gracefully after performing one replacement.

There would be 2 benefits:

  • better performance/efficiency;
  • safety, as it would prevent unwanted replacements.
@sklages
Copy link

sklages commented Nov 19, 2020

Why only the first? Maybe better - or somewhat "complete" - exit after m matches/replacements, default=1

@ngirard
Copy link
Author

ngirard commented Nov 19, 2020

@sklages, sure.

I just meant to illustrate that this feature request originated from a specific, real-world situation of mine.

@balupton
Copy link

balupton commented Dec 2, 2021

So 687f3a5 has been merged, and has added a -n flag, however it doesn't work, -n1 -n 1 -n=1 all error with:

error: Found argument '-n' which wasn't expected, or isn't valid in this context
> sd --version
sd 0.7.6

@balupton
Copy link

balupton commented Dec 2, 2021

It would be cool if the occurrence flag allowed a range, such that this becomes much easier:

echo $'a\nb\nc\na\nb\nc' | sd -n 2, '(?m:\nb$)' '' | sd '^b$' 'B'

To get back:

a
B
c
a
c

@CosmicHorrorDev CosmicHorrorDev added the M-needs triage Meta: Maintainer label me! label May 17, 2023
@CosmicHorrorDev CosmicHorrorDev added C-enhancement Category: New feature or request and removed M-needs triage Meta: Maintainer label me! labels Oct 22, 2023
@balupton
Copy link

balupton commented Nov 10, 2023

Great that -n max was added in v1.0.0 🎉

Note that the range suggestion (as teip does) has not been added: greymd/teip#27

For me, this is my use case that I want to simplify:

https://github.com/bevry/dorothy/blob/de23d08e4cb7cc7823b8638a479d86831d8f2bf8/commands/config-helper#L341-L367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants