Back to the command reference

sed

ShellAdvanced In simulator In real Linux

Stream editor: transform text with rules

Name: sed = stream editor

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

sed reads input line by line, edits it with the commands in a script and prints the result. The most used command is substitution s/pattern/replacement/flags; others include d (delete), p (print), i (insert) and a (append), each optionally limited to line numbers or regex matches.

By default sed only prints and leaves files alone; -i edits in place, so preview without it first.

Syntax

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

Common options

s/old/new/Replace the first old on each line with new
s/old/new/gReplace every old on each line
/re/dDelete lines matching re; or by number: 3d deletes line 3
N,MpPrint lines N to M (with -n: sed -n 10,20p)
-n / --quiet / --silentDon't print by default; use with the p command
-i / --in-placeEdit files in place
-e / --expression SCRIPTAdd a script; may be repeated
-E / -r / --regexp-extendedExtended regular expressions (+ ? | ( ) without backslashes)
-f / --file FILERead the sed script from FILE

Examples

  • $ sed 's/apple/APPLE/g' data/fruits.txt

    Replace every apple (prints only; file unchanged)

    Explain
  • $ sed -i 's/INFO/info/' logs/app.log

    Edit the file in place

    Explain
  • $ echo 2026-01-15 | sed 's/-/\//g'

    Change - to / in a date

    Explain

Related