Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux sed command

Reference for the Linux sed command: substitute text, delete, print and insert lines, use addresses and regex, edit files in place with -i and avoid common traps.

sed is a stream editor: it reads input one line at a time, applies a small script of editing commands to each line, and writes the result to standard output. The input file is not modified unless you ask for it with -i. Most uses are a single substitution, sed 's/old/new/', but sed can also delete, insert, print selected lines and join or reorder them.

A sed script is a list of commands, each optionally preceded by an address that says which lines it applies to (a line number, a range, or a regular expression).

Terminal
sed [OPTION]... 'SCRIPT' [FILE]...

Try it

Bash Substitution and flags

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.

Bash Addresses, delete, print and insert

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.

Syntax

Terminal
sed [-n] [-E] [-s] [-e SCRIPT]... [-f SCRIPTFILE] [-i[SUFFIX]] [FILE]...

With no -e or -f, the first non-option argument is the script. Without files sed reads standard input.

Option Description
-n Do not print lines automatically; only p and similar commands print.
-e SCRIPT Add a script; may be repeated.
-f FILE Read the script from a file.
-E, -r Use extended regular expressions (no backslashes for + ? | ( )).
-i[SUFFIX] Edit files in place, keeping a backup with SUFFIX if given.
-s Treat each file separately (line numbers restart, $ is per file).
-z Separate lines with NUL, so a whole file with newlines is one "line".
Command Description
s/RE/REPL/FLAGS Substitute. Flags: g all, N the Nth, p print if changed, I ignore case, w FILE write.
d Delete the line and start the next cycle.
p Print the line.
i\TEXT, a\TEXT, c\TEXT Insert before, append after, replace the line.
y/abc/xyz/ Transliterate characters (like tr).
= Print the line number.
q, Q Quit (after printing / without printing).
n, N Read the next line (replace / append to the pattern space).
h, H, g, G, x Hold space: copy, append, get, append get, exchange.
{ ... } Group commands under one address.
r FILE, w FILE Read a file after the line, write the line to a file.

Addresses: 5 line number, $ last line, /RE/ matching lines, A,B range, A,+N N lines after A, FIRST~STEP every STEP-th line (GNU), ! after an address negates it (2!d).

In the replacement: & is the whole match, \1...\9 are groups, \n a newline (GNU), \U, \L, \E change case (GNU).

Returns: 0 on success, 1 for an invalid command or option, 2 if an input file was missing (processing continues), 4 for an I/O error.

Usage notes

  • -i edits the original. sed -i.bak 's/a/b/' file keeps file.bak. Test without -i first. On macOS/BSD -i requires an argument (-i ''); the GNU form above does not port.
  • Basic vs extended. Default patterns are basic: \+, \?, \|, \( \) are the special forms. With -E the plain characters are special instead.
  • Greedy matching. s/<.*>// removes from the first < to the last >. Use [^>]* for a non-greedy effect: s/<[^>]*>//g.
  • Escaping the delimiter. Any character after s can be the delimiter (s|a|b|, s#a#b#). If the text contains it, escape it or pick another.
  • Quoting. Use single quotes so the shell leaves $ and \ alone. If you need a variable, use double quotes and make sure it holds no / or &.
  • Special characters in the replacement: a literal & must be written \&; a backslash \\.
  • Multi-line editing needs N or the hold space: sed ':a;N;$!ba;s/\n/,/g' joins all lines with commas. For tasks like that, paste -sd, or tr is simpler.
  • Regex addresses and s combined: sed '/^#/!s/foo/bar/' replaces on every line except comments.
  • For field-based work (columns, arithmetic) awk is a better fit; for plain searching, grep.
  • Reading and writing the same file with a redirect (sed ... f > f) empties it before sed runs. Use -i.