Quick take: The sed command is a stream editor for transforming text. Its most common use is substitution: sed 's/old/new/g' file. Add -i to edit the file in place and -n with p to print selected lines.
Introduction
The sed command (stream editor) reads text line by line and applies editing commands — substituting, deleting, inserting, or printing. It is the standard tool for non-interactive find-and-replace and for transforming files in scripts where opening an editor is impractical.
This guide focuses on the operations you will actually use: substitution, in-place editing, deleting and printing lines, and applying changes only to matching lines.
Syntax
The basic syntax of the sed command is:
sed [OPTIONS] 'COMMAND' [FILE...]Common Options and Parameters
The most useful options and parameters for the sed command:
| Option | Description |
|---|---|
| -i | Edit the file in place (add a suffix like -i.bak to keep a backup). |
| -n | Suppress automatic printing; use with the p command to print selectively. |
| -e | Add another editing command (combine several). |
| -E, -r | Use extended regular expressions. |
| -f file | Read sed commands from a file. |
| s/old/new/ | Substitute the first match of old with new on each line. |
| s/old/new/g | Substitute all matches on each line (global). |
| Nd | Delete line N; /pat/d deletes lines matching a pattern. |
| Np | Print line N (use with -n). |
Practical Examples
Real sed commands you can run today:
# Replace the first occurrence per line
sed 's/error/ERROR/' app.log
# Replace every occurrence (global)
sed 's/foo/bar/g' data.txt
# Edit a file in place, keeping a backup
sed -i.bak 's/8080/80/g' nginx.conf
# Delete blank lines
sed '/^$/d' notes.txt
# Delete lines matching a pattern
sed '/DEBUG/d' app.log
# Print only lines 5 to 10
sed -n '5,10p' file.txt
# Replace only on lines matching a pattern
sed '/^server/s/listen 80/listen 443/' site.confAdvanced sed: Ranges and Multiple Edits
Beyond single substitutions, sed can target line ranges, chain several commands, and act only on lines that match a pattern. This makes it a precise editor for configuration files.
# Substitute only between two markers
sed '/BEGIN/,/END/s/old/new/g' file
# Run several edits in one pass
sed -e 's/foo/bar/g' -e '/^#/d' -e 's/ */ /g' file
# Substitute only on lines that contain 'listen'
sed '/listen/s/80/443/' nginx.conf
# Insert a line after every match
sed '/\[mysqld\]/a max_connections = 200' my.cnfCombining an address (a line number or /pattern/) with the s command is the key to surgical edits that leave the rest of the file untouched.
Common sed Pitfalls
The biggest risk with sed is the -i flag: it edits files in place with no undo. Always test the command by letting it print to the screen first, and use -i.bak so sed keeps the original as file.bak before changing anything.
The second pitfall is escaping slashes. When your pattern or replacement contains / — as paths and URLs do — switch the delimiter instead of escaping every slash: sed 's#/old/path#/new/path#g' is far more readable than the backslash-heavy alternative. sed accepts almost any character as the delimiter right after the s.
Tips and Best Practices
- Always test without
-ifirst — run the substitution to stdout, confirm the result, then add-ito write it back. - Use
-i.bakso sed saves the original asfile.bakbefore editing, giving you a safety net. - Pick a different delimiter when your text contains slashes:
sed 's#/old/path#/new/path#g'avoids escaping.
Final Thoughts
sed is the fastest way to transform text without opening an editor — indispensable in scripts and one-off fixes alike. Master the substitution command s/old/new/g, the in-place flag -i, and line selection, and you can reshape configuration files and logs in a single line. It works beautifully alongside grep and awk.
FAQ: sed Command in Linux
How do I find and replace text with sed?+
Use the substitution command: sed 's/old/new/g' file replaces every occurrence of old with new. Drop the trailing g to replace only the first match on each line.
How do I edit a file in place with sed?+
Add the -i flag: sed -i 's/old/new/g' file changes the file directly. Use -i.bak to keep the original as file.bak first.
How do I delete lines with sed?+
Use the d command: sed '/pattern/d' file deletes lines matching the pattern, and sed '3d' deletes line 3. Add -i to apply the change to the file.
How do I print specific lines with sed?+
Combine -n with the p command: sed -n '10,20p' file prints only lines 10 through 20 and suppresses the rest.
What is the difference between sed and awk?+
sed is a line-oriented stream editor best for substitutions and simple line edits. awk is a full text-processing language that understands fields and columns, making it better for reports and column-based data.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support