sed(1)
sed is a stream editor that performs text transformations on input using regular expressions and editing commands.
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
| Flag | What it does |
|---|---|
-e SCRIPT | add script to the commands to be executed |
-f FILE | read script from file instead of command-line |
-n | suppress automatic printing of pattern space |
-i[SUFFIX] | edit files in-place; optionally backup with SUFFIX |
-E | use extended regular expressions (ERE) |
-r | alias for -E (GNU sed); use extended regex syntax |
-s | treat each file as a separate input stream |
-u | load minimal portions of input (useful for large files) |
Examples
replace first occurrence of 'old' with 'new' on each line
sed 's/old/new/' file.txtreplace all occurrences of 'old' with 'new' on each line (global flag)
sed 's/old/new/g' file.txtreplace all occurrences in-place, modifying the file directly
sed -i 's/old/new/g' file.txtprint only lines 5 through 10 (like head/tail combination)
sed -n '5,10p' file.txtdelete line 10 and print everything else
sed '10d' file.txtdelete all lines matching 'pattern'
sed '/pattern/d' file.txtadd 'prefix_' to the beginning of every line
sed 's/^/prefix_/' file.txtapply multiple substitutions in sequence
sed -e 's/old/new/' -e 's/foo/bar/' file.txt