sd
is an intuitive find & replace CLI.
Why use it over any existing tools?
Painless regular expressions
sd
uses regex syntax that you already know from JavaScript and Python. Forget about dealing with quirks of sed
or awk
- get productive immediately.
String-literal mode
Non-regex find & replace. No more backslashes or remembering which characters are special and need to be escaped.
Easy to read, easy to write
Find & replace expressions are split up, which makes them easy to read and write. No more messing with unclosed and escaped slashes.
Smart, common-sense defaults
Smart-cased regular expressions also come with a sane syntax that's not opt-in. Defaults follow common sense and are tailored for typical daily use.
While sed does a whole lot more, sd
focuses on doing just one thing and doing it well.
Some cherry-picked examples, where sd
shines:
sd before after
sed s/before/after/g
sd '\r' ','
sed ':a;N;$!ba;s/\r/,/g'
echo "sample with /path/" | sd '.*(/.*/)' '$1'
echo "sample with /path/" | sed -E 's/.*(/.*/)/\1/g'
echo "sample with /path/" | sed -E 's|.*(/.*/)|\1|g'
sd -i before after file.txt
-e
or else some platforms will consider the next argument to be a backup suffixsed -i -e 's/before/after/g' file.txt
sh
cargo install sd
-s
or --string-mode
to disable regex.```sh
echo 'lots((([]))) of special chars' | sd -s '((([])))' '' lots of special chars ```
```sh
echo 'lorem ipsum 23 ' | sd '\s+$' '' lorem ipsum 23 ```
Indexed capture groups:
```sh
echo 'cargo +nightly watch' | sd '(\w+)\s++(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3' cmd: cargo, channel: nightly, subcmd: watch ```
Named capture groups:
```sh
echo "123.45" | sd '(?P
\d+).(?P \d+)' '$dollars dollars and $cents cents' 123 dollars and 45 cents ```
In the unlikely case you stumble upon ambiguities, resolve them by using ${var}
instead of $var
. Here's an example:
```sh
echo '123.45' | sd '(?P
\d+).(?P \d+)' '$dollarsdollars and $centscents' and echo '123.45' | sd '(?P \d+).(?P \d+)' '${dollars}dollars and ${cents}cents' 123dollars and 45cents ```
```sh
sd -i 'window.fetch' 'fetch' http.js ```
That's it. The file is modified in-place.
To do a dry run:
```sh
sd 'window.fetch' 'fetch' http.js ```
Good ol' unix philosophy to the rescue.
sh
sd -i 'from "react"' 'from "preact"' $(fd -t f)
Same, but with backups (consider version control).
bash
for file in $(fd -t f); do
cp "$file" "$file.bk"
sd -i 'from "react"' 'from "preact"' "$file";
done