The sed command
The sed command edits text as it passes through, most often to find and replace…
The sed command edits text as it passes through, most often to find and replace: sed 's/old/new/g' file prints the file with every old changed to new.
Taught in Intermediate, lesson 22
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
sed [OPTION]... SCRIPT [FILE]... sed 's/OLD/NEW/g' FILE
Edit text with commands: find and replace, delete or print lines. A stream editor: reads each line, runs the SCRIPT on it and prints the result, leaving the file alone unless you add -i. The most used command is s/OLD/NEW/, which replaces the first match on each line (add g for every match). OLD is a regular expression, & in NEW stands for what matched, and \1 for the first \( \) group. A command can start with an address that picks lines: a number (3), a range (2,5), $ for the last line or a /pattern/. Other commands: d deletes the line, p prints it (use -n to print nothing else), a, i and c add, insert or change a line, y/abc/xyz/ swaps characters, q quits. Separate several commands with ; and put the script in single quotes.
How to use sed
Find and replace: s/old/new/
Without g only the first match on each line changes; with g, every match.
sed 's/pear/PEAR/' fruit.txtPEAR apple PEAR fig apple PEAR
echo 'one two one' | sed 's/one/1/g'1 two 1
Edit the file itself: sed -i
sed prints the result and leaves the file alone unless you add -i. -i.bak keeps a copy of the original.
sed -i.bak 's/Sales/Revenue/' report.txt && cat report.txt report.txt.bakQuarterly report Revenue went up 4%. Quarterly report Sales went up 4%.
Print some lines: sed -n
-n stops the normal printing, so only the lines you print appear.
sed -n '2,4p' notes.txtNote 2: renew the library books Note 3: back up the laptop Note 4: book the dentist
sed -n '/ERROR/p' app.log08:05 ERROR disk not found
Delete lines
A number, a range or a /pattern/ picks the lines; d deletes them.
sed '1d' data.csvAva,82 Leo,91 Mia,77
sed '/INFO/d' app.log08:05 ERROR disk not found 08:07 error: timeout 08:15 WARN slow request
Insert or add a line
i inserts before a line and a adds after it. A dollar sign as the address means the last line, as in $a.
sed '1i Shopping list' shopping.txtShopping list milk eggs bread butter
Use groups in the replacement
& is the whole match. With -E, ( ) makes groups and \1, \2 put them back.
sed -E 's/([A-Z][a-z]+),([0-9]+)/\2 points: \1/' data.csvname,score 82 points: Ava 91 points: Leo 77 points: Mia
Options
| -n, --quiet | print only what p (or s///p) asks for |
|---|---|
| -i[SUFFIX], --in-place[=SUFFIX] | edit the files themselves; with a suffix such as -i.bak, keep a backup |
| -e SCRIPT | add a script (use it more than once for several) |
| -E, -r | extended regular expressions: + ? | and ( ) without backslashes |
| s/OLD/NEW/g | replace every match on the line (without g, only the first; with 2, the second) |
| s/OLD/NEW/I | match upper and lower case alike |
| Nd, /re/d | delete line N, or the lines that match |
| N,Mp with -n | print lines N to M |
More examples
sed 's/pear/PEAR/g' fruit.txtPEAR apple PEAR fig apple PEAR
sed -n '3,5p' notes.txtNote 3: back up the laptop Note 4: book the dentist Note 5: pay the electricity bill
sed -i 's/Sales/Revenue/' report.txtsed -E 's/([A-Z][a-z]+),([0-9]+)/\2 \1/' data.csvname,score 82 Ava 91 Leo 77 Mia
Questions about sed
How do I replace text in a file with sed?
sed -i 's/old/new/g' file.txt changes the file itself. Try it without -i first to see the result, or use -i.bak to keep a backup.
How do I replace text that contains a slash?
Use another delimiter: sed 's|/old/path|/new/path|g'. Any character after the s works.
Why use single quotes around a sed script?
So the shell passes it unchanged. In double quotes, $ and ! and backslashes may be changed by the shell first.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.