man.lsPlain-English help for Unix commands

sed Find, replace and edit text in a stream

Reads text line by line, applies editing commands, and prints the result. Most often used to replace one piece of text with another, or to print or delete chosen lines, in a file or in piped output.

Synopsis

sed [OPTION]... 'COMMAND' [FILE]...
sed [OPTION]... -e 'COMMAND' [-f SCRIPTFILE] [FILE]...

Common options

Option or formWhat it does
-nPrint only what a p command selects. Without it, sed prints every line
-e commandAdd an editing command. Repeat -e to run several commands in order
-f fileRead the editing commands from a file
-E, -rUse extended regular expressions, so ( ), + and | work without backslashes. macOS and GNU both accept -E
-i extensionEdit the file in place. The extension is a backup suffix. macOS needs it as a separate argument, even an empty one ('')
-I extensionBSD and macOS only. Like -i, but treats all files as one continuous stream, so line numbers do not restart for each file

Examples

Replace the first match on each line
sed 's/pear/plum/' words.txt
plum
apple
plum
fig
apple
apple
Print only lines 2 to 3
sed -n '2,3p' notes.txt
beta error
gamma
Delete lines that match a pattern
sed '/error/d' notes.txt
alpha
gamma
Beta again
delta
Drop the header row of a CSV file
sed '1d' data.csv
ana,red,7
bo,blue,3
cy,red,9
dee,blue,3
Swap the first two CSV columns
sed -E 's/^([^,]*),([^,]*)/\2,\1/' data.csv
team,name,score
red,ana,7
blue,bo,3
red,cy,9
blue,dee,3
Edit a file in place and keep a backup
sed -i.bak 's/ERROR/FAIL/' app.log

Notes

See also

Last reviewed 2026-10-07.