sd/README.md

186 lines
5.5 KiB
Markdown
Raw Normal View History

# sd - `s`earch & `d`isplace
2018-12-24 00:24:43 +00:00
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.*   `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.*   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.*   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
*Smart, common-sense defaults.*   Defaults follow common sense and are tailored for typical daily use.
2018-12-30 05:04:38 +00:00
2018-12-24 00:24:43 +00:00
## Comparison to sed
While sed does a whole lot more, sd focuses on doing just one thing and doing it well. Here are some cherry-picked examples where sd shines.
2018-12-24 00:46:02 +00:00
Simpler syntax for replacing all occurrences:
- sd: `sd before after`
- sed: `sed s/before/after/g`
Replace newlines with commas:
2018-12-31 22:05:51 +00:00
- sd: `sd '\n' ','`
- sed: `sed ':a;N;$!ba;s/\n/,/g'`
Extracting stuff out of strings containing slashes:
- sd: `echo "sample with /path/" | sd '.*(/.*/)' '$1'`
- sed: `echo "sample with /path/" | sed -E 's/.*(\\/.*\\/)/\1/g'`
With sed, you can make it better with a different delimiter,
but it is still messy:
`echo "sample with /path/" | sed -E 's|.*(/.*/)|\1|g'`
In place modification of files:
2019-06-18 18:07:02 +00:00
- sd: `sd before after file.txt`
- sed: `sed -i -e 's/before/after/g' file.txt`
With sed, you need to remember to use `-e` or else some
platforms will consider the next argument to be a backup suffix.
2023-08-20 16:43:27 +00:00
2018-12-31 08:38:51 +00:00
## Benchmarks
**Simple replacement on ~1.5 gigabytes of JSON**
```sh
hyperfine --warmup 3 --export-markdown out.md \
'sed -E "s/\"/'"'"'/g" *.json > /dev/null' \
'sed "s/\"/'"'"'/g" *.json > /dev/null' \
'sd "\"" "'"'"'" *.json > /dev/null'
```
2018-12-31 08:38:51 +00:00
| Command | Mean [s] | Min…Max [s] |
|:---|---:|---:|
| `sed -E "s/\"/'/g" *.json > /dev/null` | 2.338 ± 0.008 | 2.332…2.358 |
| `sed "s/\"/'/g" *.json > /dev/null` | 2.365 ± 0.009 | 2.351…2.378 |
| `sd "\"" "'" *.json > /dev/null` | **0.997 ± 0.006** | 0.987…1.007 |
2018-12-31 08:38:51 +00:00
Result: ~2.35 times faster
**Regex replacement on a ~55M json file**:
```sh
hyperfine --warmup 3 --export-markdown out.md \
'sed -E "s:(\w+):\1\1:g" dump.json > /dev/null' \
'sed "s:\(\w\+\):\1\1:g" dump.json > /dev/null' \
'sd "(\w+)" "$1$1" dump.json > /dev/null'
2018-12-31 08:38:51 +00:00
```
| Command | Mean [s] | Min…Max [s] |
|:---|---:|---:|
| `sed -E "s:(\w+):\1\1:g" dump.json > /dev/null` | 11.315 ± 0.215 | 11.102…11.725 |
| `sed "s:\(\w\+\):\1\1:g" dump.json > /dev/null` | 11.239 ± 0.208 | 11.057…11.762 |
| `sd "(\w+)" "$1$1" dump.json > /dev/null` | **0.942 ± 0.004** | 0.936…0.951 |
2018-12-31 08:38:51 +00:00
Result: ~11.93 times faster
2018-12-24 00:24:43 +00:00
2018-12-26 21:54:59 +00:00
## Installation
Install through
[`cargo`](https://doc.rust-lang.org/cargo/getting-started/installation.html) with
`cargo install sd`, or through various package managers
2018-12-26 21:54:59 +00:00
[![Packaging status](https://repology.org/badge/vertical-allrepos/sd-find-replace.svg?exclude_unsupported=1)](https://repology.org/project/sd-find-replace/versions)
2018-12-26 07:32:26 +00:00
## Quick Guide
2018-12-24 03:57:36 +00:00
1. **String-literal mode**. By default, expressions are treated as regex. Use `-F` or `--fixed-strings` to disable regex.
2018-12-26 19:23:13 +00:00
```sh
> echo 'lots((([]))) of special chars' | sd -s '((([])))' ''
lots of special chars
```
2018-12-24 03:57:36 +00:00
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
> echo 'lorem ipsum 23 ' | sd '\s+$' ''
lorem ipsum 23
```
2018-12-24 03:57:36 +00:00
2018-12-24 04:01:20 +00:00
3. **Capture groups**
2018-12-24 03:57:36 +00:00
Indexed capture groups:
2018-12-24 03:57:36 +00:00
```sh
> echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
cmd: cargo, channel: nightly, subcmd: watch
```
2018-12-24 03:57:36 +00:00
Named capture groups:
2018-12-24 03:57:36 +00:00
```sh
> echo "123.45" | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars dollars and $cents cents'
123 dollars and 45 cents
```
2018-12-24 03:57:36 +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
> echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '$dollars_dollars and $cents_cents'
and
2023-08-20 16:43:27 +00:00
> echo '123.45' | sd '(?P<dollars>\d+)\.(?P<cents>\d+)' '${dollars}_dollars and ${cents}_cents'
123_dollars and 45_cents
```
2018-12-24 03:57:36 +00:00
2018-12-26 00:01:11 +00:00
4. **Find & replace in a file**
2018-12-24 03:57:36 +00:00
```sh
> sd 'window.fetch' 'fetch' http.js
```
2018-12-24 03:57:36 +00:00
That's it. The file is modified in-place.
2018-12-24 03:57:36 +00:00
To preview changes:
2018-12-24 03:57:36 +00:00
```sh
> sd -p '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
This example uses [fd](https://github.com/sharkdp/fd).
Good ol' unix philosophy to the rescue.
2018-12-24 03:57:36 +00:00
```sh
fd --type file --exec sd 'from "react"' 'from "preact"'
```
2018-12-24 03:57:36 +00:00
Same, but with backups (consider version control).
2018-12-24 03:57:36 +00:00
```bash
fd --type file --exec cp {} {}.bk \; --exec sd 'from "react"' 'from "preact"'
```
### Edge cases
2022-01-02 09:00:29 +00:00
sd will interpret every argument starting with `-` as a (potentially unknown) flag.
The common convention of using `--` to signal the end of flags is respected:
```bash
2022-01-02 09:00:29 +00:00
$ echo "./hello foo" | sd "foo" "-w"
error: Found argument '-w' which wasn't expected, or isn't valid in this context
USAGE:
sd [OPTIONS] <find> <replace-with> [files]...
For more information try --help
2022-01-02 09:00:29 +00:00
$ echo "./hello foo" | sd "foo" -- "-w"
./hello -w
$ echo "./hello --foo" | sd -- "--foo" "-w"
./hello -w
```
2023-08-20 16:43:27 +00:00
### Escaping special characters
To escape the `$` character, use `$$`:
```bash
echo "foo" | sd 'foo' '$$bar'
$bar
```