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

Is this possible? #285

Open
elkarouh opened this issue Dec 11, 2023 · 5 comments
Open

Is this possible? #285

elkarouh opened this issue Dec 11, 2023 · 5 comments

Comments

@elkarouh
Copy link

elkarouh commented Dec 11, 2023

I have a line
line=Note AAA: BBB: CCC:
I want to replace the ':' in all lines starting with Note with the character Z
This would make the 'sd : Z' conditional on the presence of 'Note'
Is this possible with sd ?
In sed, i would do"
sed '/^Note/s/:/Z/g' myfile

Many thanks

@nc7s
Copy link
Contributor

nc7s commented Dec 25, 2023

Side note: you can surround any lines of code or otherwise preformatted content with a pair of ``` lines. See (GitHub Flavored) Markdown.

And yes, you can do that:

❯ printf 'Note AAA:\nBBB:\nCCC:\n' | sd '^Note(.*):' 'Note${1}Z'
Note AAAZ
BBB:
CCC:

This is kinda awkward if you are used to sed syntax, but otherwise just plain old regular expression.

@elkarouh
Copy link
Author

This is not what I want to achieve.
All occurrences of ':' should be replaced by Z
In your solution, only the first occurrence is replaced.

@nc7s
Copy link
Contributor

nc7s commented Dec 25, 2023

I want to replace the ':' in all lines starting with Note with the character Z
This would make the 'sd : Z' conditional on the presence of 'Note'
Is this possible with sd ?
In sed, i would do sed '/^Note/s/:/Z/g' myfile

All occurrences of ':' should be replaced by Z
In your solution, only the first occurrence is replaced.

I don't quite follow.

@elkarouh
Copy link
Author

The result I want to get is
'Note AAA: BBB: CCC:'
should become
'Note AAAZ BBBZ CCCZ'
This is easy to achieve with sed.
I seek a solution using sd

@nc7s
Copy link
Contributor

nc7s commented Dec 26, 2023

Alright, I misunderstood ("over-interpreted" if you would) the single line example to be multiple lines misrepresented, due to the words "all lines".

As I now understand it, you want the replacement to happen iff a predicate is true. This as a general case is not possible with plain regexes (how sd currently operates). In a plain regex, after the "predicate" ^Note, you can only match a group (?:(?<span>[^:]*)?:) and replace them with ${span}Z. They can't arbitrarily stack up (not to mention the inability to express them in substitution), and a * after that group matches A: B: C: as a whole.

This is trivially solved with a little scripting if you are open to that.

If deemed necessary for sd, this probably needs a semantics along the lines of sd --line-by-line --predicate PREDICATE FIND REPLACE_WITH. Kinda verbose compared to sed, OTOH, sed syntax has been seen as cryptic. Can be implemented after #287.

I'd suggest changing the title to something like "Predicated replacements".

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

No branches or pull requests

2 participants