Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท sed Basics

sed Substitute: The s Command in Depth

Master sed's substitute command: pattern and replacement, the g, N, p and I flags, custom delimiters, the & symbol, escaping, and the most common mistakes.

If you only learn one sed command, make it s. Roughly nine out of ten sed commands in the wild are substitutions. Its form is short, but it has many small features: flags, other delimiters, special replacement characters. This lesson walks through all of them with examples you can run.

The basic form

TEXT
s/pattern/replacement/flags

sed reads a line into its pattern space, looks for the first match of pattern, replaces it with replacement, and prints the line. If there is no match the line passes through unchanged.

Bash Replace the first match

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Each line is processed separately, so printf ... | sed 's/o/0/' replaces the first o on every line. The second command shows that matching is case sensitive.

The g flag: every match

Add g to replace all matches on the line. You can also give a number to replace only the Nth match, or combine both to replace the Nth and everything after it (2g).

Bash Flags g and N

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The four results are a+b-c-d, a+b+c+d, a-b+c-d and a-b+c+d. The number counts matches on that line, starting from 1.

The p flag and -n

The p flag prints the line if a replacement happened. On its own it prints the line twice (sed already prints at the end), so it is usually paired with -n, which turns off the automatic print:

Bash Show only the lines that changed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

This sed -n 's/x/y/p' pattern is a compact "find and show what changed" tool.

The I flag: ignore case (GNU)

GNU sed offers I (also i in s) for case-insensitive matching. It is not POSIX; on BSD sed you can write the class by hand: s/[Rr][Ee][Dd]/x/.

Bash Case-insensitive matching (GNU)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The ampersand: the matched text

In the replacement, & stands for whatever the pattern matched. That lets you wrap or decorate text without retyping it. To write a literal ampersand, escape it as \&.

Bash Using & in the replacement

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first wraps each number in angle brackets, the second turns the word and into a real ampersand, and the third wraps the whole line in double quotes (.* matches everything).

Other delimiters

The slash is only a convention. Any character right after s becomes the delimiter. When your text contains slashes (paths, URLs), pick something else and avoid an unreadable forest of backslashes:

Bash Choosing a delimiter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first line works but is hard to read. |, #, : and , are popular. If you pick a delimiter that also appears in the pattern, you must escape it. The last example does that with a comma, which shows why it is not the best choice.

Special characters in the replacement

Written in the replacement Result
& the whole match
\1 ... \9 the text of a captured group (next lessons)
\& a literal ampersand
\\ a literal backslash
\n a newline (GNU, and POSIX allows \ followed by a real newline)
the delimiter, escaped a literal delimiter
Bash Newlines in a replacement

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Special characters in the pattern

The shell, sed and the regex all have an opinion about special characters. A few reminders:

  • . * [ ] ^ $ \ are special in the pattern. Escape them to match literally: s/3\.14/pi/.
  • In BRE (default) + ? | ( ) { } are literal. Use sed -E to make them special.
  • Single quotes protect the whole expression from the shell. To use a $variable in a sed command, switch to double quotes and think about escaping, or better, validate the variable first (see Real-world Tasks).
Bash Escaping and the shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The second command shows the classic bug: an unescaped dot matches any character, so 3x14 changes too. The third is correct. The last uses double quotes so the shell inserts the variable. That is fine for trusted values, but if $name contained / or & the command would break or behave strangely.

Common mistakes

  1. Forgetting g: only the first match per line changes.
  2. Greedy .*: s/a.*b/X/ eats everything between the first a and the last b. Use [^b]*.
  3. Expecting -i behaviour: without -i, the file is not changed.
  4. Regex vs literal: a dot, star or bracket in the search text must be escaped.
  5. Special characters in replacement: a stray & or \1 is interpreted.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the result of echo a-b-c | sed 's/-/+/2'?

    A number flag replaces only that occurrence: the second - on the line.
  2. 2. What does & mean in the replacement part?

    & is replaced by the text the pattern matched. Write \& for a literal ampersand.
  3. 3. Why use s|/usr/local|/opt| instead of slashes?

    sed uses the character after s as the delimiter, so choosing one that does not appear in your text keeps the command readable.
  4. 4. What do -n and the p flag do together in sed -n 's/x/y/p'?

    -n turns off the automatic printing, and p prints a line only if the substitution succeeded.

Summary

  • s/pattern/replacement/flags replaces the first match on each line.
  • Flags: g (all), a number (the Nth), p (print), I (ignore case, GNU).
  • & is the whole match; escape it as \& for a literal ampersand.
  • Choose another delimiter when the text contains slashes.
  • Escape special characters in the pattern and watch greedy .*.

Next: Printing and Deleting Lines