sd (with examples)
Use Case 1: Trim whitespace using a regular expression
Code:
echo 'lorem ipsum 23 ' | sd '\s+$' ''
Motivation:
Sometimes, when working with text, there may be extra whitespace at the end of a line that needs to be removed. This can be particularly problematic when processing data or when comparing strings. The sd
command provides an intuitive way to find and replace patterns using regular expressions, making it easy to trim whitespace from the end of a line.
Explanation:
echo 'lorem ipsum 23 '
: This command will output the given text to the standard output.|
: This is a pipe operator that takes the output from the previous command and passes it as input to the next command.sd '\s+$' ''
: This is thesd
command, which is used to find and replace patterns. In this case, it uses the regular expression\s+$
to match one or more whitespace characters at the end of a line ($
matches the end of a line). The empty string''
is used as the replacement, effectively removing the matched whitespace.
Example Output:
lorem ipsum 23
Use Case 2: Replace words using capture groups
Code:
echo 'cargo +nightly watch' | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
Motivation:
There may be situations where you need to extract specific information from a string and reformat it into a different structure. This can be useful when parsing data or when transforming strings into a different representation. The sd
command allows for find and replace operations using capture groups, making it easy to extract and rearrange parts of a string.
Explanation:
echo 'cargo +nightly watch'
: This command will output the given text to the standard output.|
: This is a pipe operator that takes the output from the previous command and passes it as input to the next command.sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
: This is thesd
command, which is used to find and replace patterns. In this case, it uses the regular expression(\w+)\s+\+(\w+)\s+(\w+)
to match three words separated by whitespace and a plus sign. The capture groups(\w+)
capture each word separately. The replacement'cmd: $1, channel: $2, subcmd: $3'
reorganizes the words using the capture group references$1
,$2
, and$3
.
Example Output:
cmd: cargo, channel: nightly, subcmd: watch
Use Case 3: Find and replace in a specific file
Code:
sd -p 'window.fetch' 'fetch' path/to/file.js
Motivation:
In a codebase, there may be instances where a specific pattern needs to be replaced in a single file. This can be useful when updating code from one version to another or when fixing a specific bug or behavior. The sd
command allows for finding and replacing patterns in a specific file, simplifying the process of making targeted changes.
Explanation:
sd -p 'window.fetch' 'fetch' path/to/file.js
: This is thesd
command with the argument-p
, which tellssd
to treat the following argument as the pattern to search for. The pattern is'window.fetch'
. The next argument'fetch'
is the replacement string. The final argumentpath/to/file.js
specifies the file to perform the find and replace operation on.
Example Output:
The content of path/to/file.js
:
window.fetch(url);
After running the command:
fetch(url);
Use Case 4: Find and replace in all files in the current project
Code:
sd 'from "react"' 'from "preact"' "$(find . -type f)"
Motivation:
When working on a project with multiple files, there may be instances where a specific pattern needs to be replaced across all files. This can be useful when switching libraries, updating imports, or fixing common mistakes. The sd
command allows for finding and replacing patterns across all files in a project, streamlining the process of making consistent changes.
Explanation:
sd 'from "react"' 'from "preact"'
: This is thesd
command, where the first argument'from "react"'
is the pattern to search for and the second argument'from "preact"'
is the replacement string.$(find . -type f)
: This command substitution is enclosed in$()
, which allows the output of thefind
command to be used as an argument tosd
. Thefind
command is used to locate all files (-type f
) in the current directory (.
).
Example Output:
Assuming there are three files in the current project:
file1.js:
import { Component } from "react";
file2.js:
import { useState } from "react";
import { useEffect } from "react";
file3.js:
import { render } from "react-dom";
After running the command, all occurrences of 'from "react"'
will be replaced with 'from "preact"'
in all files:
file1.js:
import { Component } from "preact";
file2.js:
import { useState } from "preact";
import { useEffect } from "preact";
file3.js:
import { render } from "preact-dom";