Speaking of regex: sd - a tool to search and displace

With all its peculiarities of syntax, sed leaves a bit to be desired. That’s why I was pleased to find sd, or “Search and Displace”, a tool that does what sed does but with less arcane syntax.

For example:

# prints "Corruption" (W.Barr)
echo "\"Corruption\" by W.Barr" | sd '(.+)\sby\s(.+)' '$1 ($2)'

Or just leave out the \s metacharacter in favour of a literal string:

sd '(.+) by (.+)' '$1 ($2)'

It does all the tricks you’d expect from a sed replacement. For example, named capture groups:

echo “The U.S. president is Donald Trump” | sd ‘.+\s(?P\w+ \w+)$’ ‘$evilhuman is a liar.’

# prints Donald Trump is a liar.
echo "The U.S. president is Donald Trump" | sd '.+\s(?P<evilhuman>\w+ \w+)$' '$evilhuman is a liar.'

The published benchmarks suggest that it is comparably performant, if not superior to sed.

It’s work checking out - chmln/sd. Installation on macOS is simple, just brew install sd if you’re using Homebrew.