$linuxjunkies
>

sed(1)

sed is a stream editor that performs text transformations on input using regular expressions and editing commands.

UbuntuDebianFedoraArch

Synopsis

sed [OPTION]... {script} [FILE]...

Description

sed reads input line-by-line, applies a script of editing commands to each line, and outputs the result. It's commonly used for find-and-replace operations, deleting lines, inserting text, and transforming file content without opening an editor.

The script can be provided as a command-line argument (with -e) or read from a file (with -f). sed operates on patterns and ranges, executing commands only on matching lines unless otherwise specified.

By default, sed prints all processed lines. Use -n to suppress automatic printing and print only lines you explicitly request with p or similar commands.

Common options

FlagWhat it does
-e SCRIPTadd script to the commands to be executed
-f FILEread script from file instead of command-line
-nsuppress automatic printing of pattern space
-i[SUFFIX]edit files in-place; optionally backup with SUFFIX
-Euse extended regular expressions (ERE)
-ralias for -E (GNU sed); use extended regex syntax
-streat each file as a separate input stream
-uload minimal portions of input (useful for large files)

Examples

replace first occurrence of 'old' with 'new' on each line

sed 's/old/new/' file.txt

replace all occurrences of 'old' with 'new' on each line (global flag)

sed 's/old/new/g' file.txt

replace all occurrences in-place, modifying the file directly

sed -i 's/old/new/g' file.txt

print only lines 5 through 10 (like head/tail combination)

sed -n '5,10p' file.txt

delete line 10 and print everything else

sed '10d' file.txt

delete all lines matching 'pattern'

sed '/pattern/d' file.txt

add 'prefix_' to the beginning of every line

sed 's/^/prefix_/' file.txt

apply multiple substitutions in sequence

sed -e 's/old/new/' -e 's/foo/bar/' file.txt

Related commands