NOTE

This is also valid for vim (POSIX-like with minor extensions — behavior is nearly identical)

INFO

sed has two modes:

  • BRE (Basic Regular Expressions) — default, sed 'pattern'
  • ERE (Extended Regular Expressions) — activated with -E flag, sed -E 'pattern'

The main difference is how special characters are escaped (see each section below).


Shorthand Character Classes

Unlike VSCode/Perl, POSIX does not support \d, \w, \s. Use POSIX named classes inside [[:class:]] instead:

POSIX ClassMatchesVSCode equivalent
[[:digit:]]Any digit (0-9)\d
[[:alpha:]]Any letter (a-z, A-Z)
[[:alnum:]]Letters and digits\w (approx.)
[[:space:]]Any whitespace\s
[[:upper:]]Uppercase letters
[[:lower:]]Lowercase letters
[[:punct:]]Punctuation characters

Example:

sed 's/[[:digit:]]//g' file.txt   # remove all digits

Anchors

PatternMeaning
^Start of line
$End of line
[^...]Negation inside a class. Example: [^0-4] excludes digits 0-4

Example:

sed 's/^/>>> /' file.txt    # prepend >>> to every line
sed 's/$/;/' file.txt       # append ; to every line

Wildcards & Quantifiers

MeaningBREERE (-E)
Any single character..
0 or 1 (optional)\??
1 or more\++
0 or more**
Between n and m\{n,m\}{n,m}

Key rule: in BRE, quantifiers need a backslash: \+, \?, \{n,m\}.

In ERE (-E), they work without: +, ?, {n,m}.

Example:

sed -E 's/[0-9]+/NUM/g' file.txt    # ERE syntax
sed 's/[0-9]\+/NUM/g' file.txt      # BRE syntax (same result)

Boundaries

POSIX does not support \b. Use \< and \> instead:

PatternMatches
\<calmWords that start with calm
calm\>Words that end with calm
\<calm\>Exactly the word calm and nothing else

Example:

sed 's/\<calm\>/PEACE/g' file.txt

Groups & Backreferences

Groups are defined with \(...\) in BRE and (...) in ERE. Captured groups can be referenced with \1, \2, etc.

Example — swap two words:

sed -E 's/(hello) (world)/\2 \1/' file.txt
# Output: "world hello"

Example — alternation (ERE only):

sed -E 's/(High|Mid|Low)-level/LEVEL/g' file.txt

Note: Alternation with | requires -E in SED. In BRE it is not supported natively.


SED Command Structure

sed [options] 's/PATTERN/REPLACEMENT/FLAGS' file.txt
ComponentDescription
sSubstitution command (most common)
gReplace all occurrences per line (not just the first)
iCase-insensitive match
-EEnable ERE syntax
-iEdit file in place (overwrites the original)

Example:

sed -E -i 's/foo/bar/g' file.txt    # replace all "foo" with "bar" in-place

Exercises

Remove blank lines from a file

sed '/^$/d' file.txt

Match a phone number

sed -E 's/[0-9]{3}-[0-9]{2}-[0-9]{4}/PHONE/g' file.txt

Match an IPv4 address

sed -E 's/(([0-9]{1,3}\.){3}[0-9]{1,3})/IP/g' file.txt

Note: Use \< / \> instead of \b for strict POSIX compliance.