$linuxjunkies
>

sd(1)

A modern, user-friendly replacement for sed that finds and replaces text with regex support and intuitive syntax.

UbuntuDebianFedoraArch

Synopsis

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

Description

sd is a command-line search and replace utility written in Rust that simplifies text substitution. It uses regex patterns by default and provides intuitive flags that are easier to remember than sed's cryptic options.

When no files are specified, sd reads from standard input. The pattern supports Perl-compatible regular expressions with capture groups, and the replacement string can reference captured groups using $1, $2, etc.

By default, sd only replaces the first match on each line; use -g to replace all occurrences globally.

Common options

FlagWhat it does
-g, --globalReplace all occurrences on each line, not just the first
-i, --in-placeEdit files in place, overwriting the original
-p, --previewPrint what would be replaced without making changes
-c, --colorColorize output (on by default when printing to terminal)
-s, --string-modeTreat pattern and replacement as literal strings, not regex
-F, --fixed-stringsAlias for --string-mode
-n, --no-regexDisable regex matching
-e, --patternSpecify the search pattern (useful when pattern starts with dash)

Examples

Replace first occurrence of 'foo' with 'bar' on each line, print result to stdout

sd 'foo' 'bar' file.txt

Replace all occurrences of 'foo' with 'bar' on each line

sd -g 'foo' 'bar' file.txt

Remove trailing whitespace from all lines in file.txt (in-place edit)

sd -i '\s+$' '' file.txt

Preview replacing email addresses with 'user at domain' format using capture groups

sd -p '(\w+)@(\w+)' '$1 at $2' emails.txt

Replace 'old' with 'new' reading from piped input

cat file.txt | sd 'old' 'new'

Treat pattern and replacement as literal strings (ignore special regex characters)

sd -s 'cost: $50' 'cost: $60' invoice.txt

Case-insensitive replacement using inline regex flag (?i)

sd '(?i)version' 'VERSION' file.txt

Replace all 'TODO' with 'DONE' in all markdown files (in-place)

sd -i -g 'TODO' 'DONE' *.md

Related commands