- Introduction
sed (Stream Editor) is a powerful Unix tool used for:
- searching
- filtering
- substituting
- modifying text
- batch editing files
sed works on a stream of text line-by-line.
- It is especially powerful for automation, scripting, and command-line text processing.
- Basic Syntax
sed 'command' filename
- Commands are placed inside single quotes.
sed supports many editing commands; the most common is s for substitution.
- Print File Contents (Default Behavior)
sed '' file.txt
sed always prints each processed line unless told otherwise.
'' means "no modifications."
- Substitution Command (
s)
sed 's/old/new/' file.txt
- Replaces the first occurrence of
old with new in each line.
- Global Replacement (
g)
sed 's/foo/bar/g' file.txt
g means "replace all occurrences per line."
- Without
g, only the first match is replaced.
- Using Different Delimiters
sed 's|/usr/local|/opt/apps|' file.txt
| or # can be used as delimiters when the text contains slashes.
- Makes commands much easier to read.
- Edit File In-Place (
-i)
sed -i 's/foo/bar/g' file.txt
- Modifies the file directly.
- Use cautiously: changes cannot be undone unless you specify backup.
sed -i.bak 's/foo/bar/g' file.txt
- Creates
file.txt.bak before modifying.
- Print Specific Lines
sed -n '3p' file.txt
-n disables automatic printing.
3p prints line 3.
sed -n '5,8p' file.txt # print lines 5 to 8
- Delete Lines
sed '3d' file.txt
sed '2,5d' file.txt # delete lines 2 to 5
- Useful for cleaning text files or logs.
- Insert and Append Lines
sed '3i This is an inserted line' file.txt
sed '3a This is an appended line' file.txt
- Replace a Whole Line
sed '4c This replaces line 4 completely' file.txt
c replaces an entire line with new content.
- Using Patterns Instead of Line Numbers
sed '/error/d' file.log
- Deletes all lines containing the word
error.
sed '/BEGIN/,/END/p' config.txt
- Works with ranges defined by patterns.
- Multiple Commands
sed -e 's/foo/bar/' -e 's/x/y/' file.txt
- Apply multiple operations in order.
sed '
s/foo/bar/g
s/apple/orange/g
' file.txt
- Commands can also be written on multiple lines.
- Advanced Example: Remove Empty Lines
sed '/^$/d' file.txt
- Advanced Example: Remove Trailing Spaces
sed 's/[ ]*$//' file.txt
- Replaces trailing spaces with nothing.
- Advanced Example: Replace Only on Matching Lines
sed '/error/ s/failed/FAILED/' file.txt
- Only lines containing
error will have failed replaced.
- Hold Space and Pattern Space (For Advanced Users)
sed maintains two memory areas:
- pattern space — the current line
- hold space — temporary buffer
- Commands like
h, H, g, G allow advanced line manipulation.
sed 'h; $!d; x' file.txt
- This swaps first and last lines (example of advanced usage).
- Summary of Common
sed Commands
| Command |
Description |
Example |
s/a/b/ |
Substitute first occurrence |
s/foo/bar/ |
s/a/b/g |
Substitute all occurrences |
s/old/new/g |
p |
Print |
-n '3p' |
d |
Delete |
'/error/d' |
i |
Insert before |
'3i text' |
a |
Append after |
'3a text' |
c |
Replace line |
'4c new text' |
-i |
Edit file in-place |
-i 's/a/b/' file |