#75·sd

Pass matched string to command

Author: mb720Created Apr 28, 2020Updated Feb 20, 2026
LabelsC-enhancement

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?