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

Pass matched string to command #75

Open
mb720 opened this issue Apr 28, 2020 · 6 comments
Open

Pass matched string to command #75

mb720 opened this issue Apr 28, 2020 · 6 comments
Labels
M-needs triage Meta: Maintainer label me!

Comments

@mb720
Copy link

mb720 commented Apr 28, 2020

Hi and thanks for sd!

I was wondering if it's possible with sd to have a command like date create the replacement string, where date gets the matched string as an argument.

I hope I can clarify this with an example.

Assume you have a file that contains dates formatted according to ISO 8601:

Something important happened on 1999-12-31.
On 2019-06-28, I bought ice cream for $3.50.
The previous leap day was on 2020-02-28.

For some reason, all these dates need to be incremented by one day. After running sd, the last sentence should be

The previous leap day was on 2020-02-29.

It's easy to add a day with date:

date -d "2020-02-28 + 1 day" --iso-8601

produces 2020-02-29.

I'd love to use that date command with sd to increment the dates in a file:

sd "\d{4}-\d\d-\d\d" "{date -d "& + 1 day" --iso-8601}" file_with_dates

In this made-up command, I've used & as a backreference to the string matched by the regex \d{4}-\d\d-\d\d, i.e., ISO 8601 dates.

I've used curly braces in the second argument to sd to signify that it's a command whose output sd should use as the replacement string.

Is something like this possible with sd (or sed)?

Does somebody else crave that feature?

@pvinis
Copy link

pvinis commented Aug 3, 2020

Seems like both sed and sd can do the cmd output thing, but it also seems none of them can handle cmd output and capture groups. :(

@mb720
Copy link
Author

mb720 commented Aug 4, 2020

@pvinis: When you say the cmd output thing is possible, do you mean sed or sd can do what I described in my original comment? The equivalent of sd "\d{4}-\d\d-\d\d" "{date -d "& + 1 day" --iso-8601}" file_with_dates to increment all dates in the file by one day using the output of date? If so, what's the specific sed/sd command?

@pvinis
Copy link

pvinis commented Aug 4, 2020

I guess like https://stackoverflow.com/a/17301724/377456. That works for you, right? Using $(date -d ..).

I needed the capture too, so I ended up using perl https://github.com/pvinis/home-dashboard/blob/master/scripts/copy-env-from-1pass#L3.

@mb720
Copy link
Author

mb720 commented Aug 4, 2020

Thanks, but that doesn't work for me. I'm trying to pass the matched string to date:

sed -E "s/[0-9]{4}-[0-9]{2}-[0-9]{2}/$(date -d "& + 1 day" --iso-8601)/" file_with_dates.txt

Here, the ampersand should be replaced with the matched string (a date in this case). But it's a literal ampersand that gets passed to date.

@pvinis
Copy link

pvinis commented Aug 4, 2020

I would recommend you use perl too then.

Take this as a base, and go crazy :)

perl -pi -e 's/(.*)/`date -d $2`/ge' file.txt

@balupton
Copy link

balupton commented Dec 2, 2021

ripgrep like sd also uses the same rust regex engine https://docs.rs/regex/latest/regex/ perhaps its --only-matching, --replace, --passthru flags could be useful

https://github.com/greymd/teip may also be useful

@CosmicHorrorDev CosmicHorrorDev added the M-needs triage Meta: Maintainer label me! label May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
M-needs triage Meta: Maintainer label me!
Projects
None yet
Development

No branches or pull requests

4 participants