2018-12-24 00:24:43 +00:00
# sd - s[earch] & d[isplace]
2018-12-26 00:01:11 +00:00
`sd` is an intuitive find & replace CLI.
2018-12-24 00:24:43 +00:00
2018-12-26 00:01:11 +00:00
## The Pitch
Why use it over any existing tools?
2018-12-24 00:24:43 +00:00
**Painless regular expressions**
2018-12-26 00:01:11 +00:00
`sd` uses regex syntax that you already know from JavaScript and Python. Forget about dealing with quirks of `sed` or `awk` - get productive immediately.
2018-12-24 00:24:43 +00:00
**String-literal mode**
2018-12-26 07:32:26 +00:00
Non-regex find & replace. No more backslashes or remembering which characters are special and need to be escaped.
2018-12-24 00:24:43 +00:00
**Easy to read, easy to write**
2018-12-26 21:36:08 +00:00
Find & replace expressions are split up, which makes them easy to read and write. No more messing with unclosed and escaped slashes.
2018-12-24 00:24:43 +00:00
2018-12-30 05:04:38 +00:00
**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.
**High performance**
Multiple times faster than `sed` beyond trivial cases. (Benchmarks incoming).
2018-12-24 00:24:43 +00:00
## Comparison to sed
2018-12-26 00:01:11 +00:00
While sed does a whole lot more, `sd` focuses on doing just one thing and doing it well.
2018-12-24 00:46:02 +00:00
2018-12-24 00:24:43 +00:00
Some cherry-picked examples, where `sd` shines:
2018-12-27 00:42:58 +00:00
- Simpler syntax for replacing all occurrences:
- sd: `sd before after`
- sed: `sed s/before/after/g`
2018-12-24 00:24:43 +00:00
- Replace newlines with commas:
2018-12-26 19:23:13 +00:00
- sd: `sd '\r' ','`
2018-12-30 05:04:38 +00:00
- sed: `sed ':a;N;$!ba;s/\r/,/g'`
2018-12-27 00:42:58 +00:00
- Extracting stuff out of strings containing slashes:
- sd: `echo "sample with /path/" | sd '.*(/.*/)' '$1'`
2018-12-30 05:04:38 +00:00
- sed: use different delimiters every time depending on expression so that the command is not completely unreadable
- `echo "sample with /path/" | sed -E 's/.*(/.*/)/\1/g'`
2018-12-27 00:42:58 +00:00
- `echo "sample with /path/" | sed -E 's|.*(/.*/)|\1|g'`
- In place modification of files:
2018-12-30 05:04:38 +00:00
- sd: `sd -i before after file.txt`
- sed: you need to remember to use `-e` or else some platforms will consider the next argument to be a backup suffix
2018-12-27 00:42:58 +00:00
- `sed -i -e 's/before/after/g' file.txt`
2018-12-24 00:24:43 +00:00
2018-12-26 21:54:59 +00:00
## Installation
2018-12-28 00:39:53 +00:00
### Cargo
2018-12-26 21:54:59 +00:00
```sh
cargo install sd
```
2018-12-30 05:04:38 +00:00
### Arch Linux
2018-12-28 00:39:53 +00:00
2018-12-30 05:04:38 +00:00
[AUR package for sd ](https://aur.archlinux.org/packages/sd/ ).
2018-12-28 00:39:53 +00:00
2018-12-26 07:32:26 +00:00
## Quick Guide
2018-12-24 03:57:36 +00:00
2018-12-26 19:23:13 +00:00
1. **String-literal mode** . By default, expressions are treated as regex. Use `-s` or `--string-mode` to disable regex.
2018-12-24 03:57:36 +00:00
```sh
2018-12-26 19:23:13 +00:00
> echo 'lots((([]))) of special chars' | sd -s '((([])))' ''
2018-12-24 03:57:36 +00:00
lots of special chars
```
2018-12-24 04:01:20 +00:00
2. **Basic regex use** - let's trim some trailing whitespace
2018-12-24 03:57:36 +00:00
```sh
2018-12-26 19:23:13 +00:00
> echo 'lorem ipsum 23 ' | sd '\s+$' ''
2018-12-24 03:57:36 +00:00
lorem ipsum 23
```
2018-12-24 04:01:20 +00:00
3. **Capture groups**
2018-12-24 03:57:36 +00:00
Indexed capture groups:
```sh
2018-12-26 19:23:13 +00:00
> echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
2018-12-24 03:57:36 +00:00
cmd: cargo, channel: nightly, subcmd: watch
```
Named capture groups:
```sh
2018-12-26 19:23:13 +00:00
> echo "123.45" | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars dollars and $cents cents'
2018-12-24 03:57:36 +00:00
123 dollars and 45 cents
```
2018-12-26 00:01:11 +00:00
In the unlikely case you stumble upon ambiguities, resolve them by using `${var}` instead of `$var` . Here's an example:
2018-12-24 03:57:36 +00:00
```sh
2018-12-26 19:23:13 +00:00
> echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars_dollars and $cents_cents'
2018-12-24 03:57:36 +00:00
and
2018-12-26 19:23:13 +00:00
> echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '${dollars}_dollars and ${cents}_cents'
2018-12-24 03:57:36 +00:00
123_dollars and 45_cents
```
2018-12-26 00:01:11 +00:00
4. **Find & replace in a file**
2018-12-24 03:57:36 +00:00
```sh
2018-12-29 16:52:50 +00:00
> sd -i 'window.fetch' 'fetch' http.js
2018-12-24 03:57:36 +00:00
```
2018-12-26 00:01:11 +00:00
That's it. The file is modified in-place.
2018-12-24 03:57:36 +00:00
2018-12-29 16:52:50 +00:00
To do a dry run:
2018-12-24 03:57:36 +00:00
```sh
2018-12-29 16:52:50 +00:00
> sd 'window.fetch' 'fetch' http.js
2018-12-24 03:57:36 +00:00
```
2018-12-24 04:01:20 +00:00
5. **Find & replace across project**
2018-12-24 03:57:36 +00:00
Good ol' unix philosophy to the rescue.
```sh
2018-12-31 06:33:30 +00:00
sd -i 'from "react"' 'from "preact"' $(fd -t f)
2018-12-24 03:57:36 +00:00
```
Same, but with backups (consider version control).
```bash
for file in $(fd -t f); do
cp "$file" "$file.bk"
2018-12-29 16:52:50 +00:00
sd -i 'from "react"' 'from "preact"' "$file";
2018-12-24 03:57:36 +00:00
done
```