LinuxCommandLibrary

sd

Search and replace strings within files

TLDR

Trim some whitespace using a regex (output stream: stdout)

$ [echo 'lorem ipsum 23 '] | sd '\s+$' ''
copy

Replace words using capture groups (output stream: stdout)
$ [echo 'cargo +nightly watch'] | sd '(\w+)\s+\+(\w+)\s+(\w+)' 'cmd: $1, channel: $2, subcmd: $3'
copy

Find and replace in a specific file (output stream: stdout)
$ sd [[-p|--preview]] ['window.fetch'] ['fetch'] [path/to/file.js]
copy

Find and replace in all files in the current project (output stream: stdout)
$ sd ['from "react"'] ['from "preact"'] "$(find . -type f)"
copy

SYNOPSIS

sd [OPTIONS] <PATTERN> <REPLACEMENT> [FILES...]

PARAMETERS

<PATTERN>
    The regular expression or literal string to search for within the input.

<REPLACEMENT>
    The string that will replace all occurrences of the pattern. Can include capture groups from the pattern.

[FILES...]
    One or more paths to files that sd should process. If no files are specified, sd reads from standard input.

-s, --string-mode
    Treat PATTERN and REPLACEMENT as literal strings instead of regular expressions.

-i, --in-place
    Modify files directly in place. By default, this creates a backup of the original files (e.g., .bak).

-p, --preview
    Show a diff-like preview of changes without modifying files. This option implies --in-place.

-f <flags>, --flags <flags>
    Apply specific regular expression flags. Common flags include 'i' for case-insensitive matching, 'm' for multi-line mode, and 's' for dotall mode.

-n, --dry-run
    Perform the find and replace operation and print the result to standard output, but do not write any changes to files.

-h, --help
    Display help information for sd and exit.

-V, --version
    Display the current version of sd and exit.

DESCRIPTION

sd is a modern, intuitive, and extremely fast command-line tool for finding and replacing strings. Written in Rust, it offers a user-friendly alternative to traditional tools like sed for common text manipulation tasks. It supports powerful regular expressions for pattern matching and replacement, allowing for complex transformations. sd can process input from standard input or directly edit files, and includes features like case-insensitivity, global replacement, and an optional preview mode to review changes before they are applied. Its primary goal is to provide a highly performant and easy-to-use solution for everyday text transformations.

CAVEATS

sd is not a standard utility included with most Linux distributions. It must be installed separately, typically via the Rust package manager cargo or from specific distribution repositories. Its regular expression syntax might differ slightly from sed or Perl due to using Rust's regex crate. When using --in-place, sd by default creates backup files with a .bak extension, which users might need to manage or remove manually.

PERFORMANCE

One of sd's key features is its exceptional speed. Leveraging Rust's performance capabilities, it often processes large files and complex patterns significantly faster than traditional stream editors like sed, making it ideal for large-scale text transformations.

REGEX ENGINE

sd utilizes the powerful and highly optimized regex crate in Rust for its pattern matching. This engine supports a feature set similar to Perl-compatible regular expressions (PCRE) while offering robust performance and memory safety.

HISTORY

sd was developed by Tatsuya Kawasaki (chmln) as a modern, fast, and user-friendly alternative to the traditional sed command. Its primary motivation was to provide a command-line utility for find-and-replace operations that is both highly performant (being written in Rust) and easier to use for common tasks, without sacrificing the power of regular expressions. It emerged as part of a trend towards creating faster, more intuitive CLI tools using modern programming languages.

SEE ALSO

sed(1), awk(1), grep(1), perl(1)

Copied to clipboard