sed Command in Linux — Stream Editor with Practical Examples
sed (stream editor) is a Linux command that reads text line by line, applies editing operations, and writes the result to standard output. Unlike interactive editors like vim or nano, sed is non-interactive — perfect for scripting, batch processing, and automation.
What You’ll Learn
By the end of this tutorial, you’ll know how to use sed for text substitution with global and per-line flags, delete lines by number or pattern, insert and append text, edit files in-place, chain multiple commands, use address ranges, and capture groups with regex backreferences.
Why sed Matters
sed is one of the fastest ways to transform text files without opening them. Configuration file changes, log sanitization, data cleaning, and mass find-and-replace across hundreds of files — these are all sed tasks. DodaZIP uses sed in its build pipeline to update version strings across configuration files, and Durga Antivirus Pro uses it to sanitize log data before analysis.
sed Learning Path
flowchart LR
A[grep Command] --> B[sed Command<br/>You are here]
B --> C[awk Command]
C --> D[xargs Command]
D --> E[Shell Scripting]
style B fill:#f90,color:#fff
Syntax Overview
sed [options] 'command' file...
sed [options] -f script_file file...| Option | Description |
|---|---|
-n | Suppress automatic printing (used with p flag) |
-i | Edit file in-place (optional backup: -i.bak) |
-e | Chain multiple commands |
-f | Read commands from a script file |
-E | Extended regex (capture groups without escaping ()) |
10 Practical Examples
Create a sample file to follow along:
cat > data.txt << 'EOF'
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080
EOF1. Basic Substitution
Replace first occurrence of “fox” with “cat” on each line:
sed 's/fox/cat/' data.txtLine 1: The quick brown cat
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown cat
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=80802. Global Flag — Replace All Occurrences
Replace ALL occurrences of “o” with “0” on each line:
sed 's/o/0/g' data.txtLine 1: The quick br0wn f0x
Line 2: jumps 0ver the lazy d0g
Line 3: THE QUICK BR0WN F0X
Line 4: jumps 0ver the lazy d0g again
Line 5: Repeat the quick br0wn f0x
Line 6: Indented line here
Line 7:
Line 8: # This is a c0mment
Line 9: server=l0calh0st
Line 10: p0rt=80803. Delete Lines by Number
Delete line 3:
sed '3d' data.txtLine 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080Delete lines 3 through 5:
sed '3,5d' data.txtLine 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=80804. Delete Lines by Pattern
Delete all lines starting with #:
sed '/^#/d' data.txtLine 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 7:
Line 9: server=localhost
Line 10: port=8080Delete empty lines:
sed '/^$/d' data.txtLine 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=80805. Insert and Append Lines
Insert a line BEFORE line 5:
sed '5i\Line inserted before line 5' data.txtLine 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line inserted before line 5
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080Append a line AFTER line 10:
sed '10a\Line appended after line 10' data.txtLine 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080
Line appended after line 106. In-Place Editing
Edit the file directly (creates backup data.txt.bak):
sed -i.bak 's/localhost/127.0.0.1/' data.txt
cat data.txtLine 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=127.0.0.1
Line 10: port=8080Restore the backup:
mv data.txt.bak data.txt7. Multiple Commands with -e
Replace “fox” with “cat” AND delete comment lines in one pass:
sed -e 's/fox/cat/' -e '/^#/d' data.txtLine 1: The quick brown cat
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown cat
Line 6: Indented line here
Line 7:
Line 9: server=localhost
Line 10: port=80808. Address Ranges
Print only lines 2 through 4:
sed -n '2,4p' data.txtLine 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog againSubstitute only on lines 1 through 3:
sed '1,3s/fox/cat/' data.txtLine 1: The quick brown cat
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=80809. Regex Capture Groups
Swap word order — capture “quick brown” and reinsert reversed:
sed -E 's/(quick) (brown)/\2 \1/' data.txtLine 1: The brown quick fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the brown quick fox
Line 6: Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080Note: Line 3 is uppercase so it didn’t match.
10. Print Matching Lines Only
Print lines containing “ERROR” from a log:
cat > app.log << 'EOF'
INFO: Server started
ERROR: Connection refused
WARN: Disk full
ERROR: Timeout
EOF
sed -n '/ERROR/p' app.logERROR: Connection refused
ERROR: TimeoutCommon Use Cases
Remove Trailing Whitespace
sed -i 's/[[:space:]]*$//' file.txtConvert Line Endings (DOS to Unix)
sed -i 's/\r$//' file.txtExtract Lines Between Two Markers
sed -n '/START/,/END/p' file.txtComment Out Lines Matching a Pattern
sed -i '/pattern/s/^/# /' config.cfgCommon Mistakes
1. Forgetting the -i Flag Without a Backup
sed -i 's/foo/bar/' file.txt modifies the file directly. Always use -i.bak the first time to create a backup.
2. Confusing s/// with Address Ranges
s/old/new/ is the substitute command. 3,5d uses an address range (lines 3-5) with the delete command. They are different operations.
3. Not Escaping Forward Slashes in Patterns
If your pattern contains /, use a different delimiter: s#/usr/bin#/usr/local/bin#g.
4. Using -n Without p
sed -n suppresses output. Without the p flag, nothing prints. Always use sed -n '/pattern/p' to see matches.
Practice Questions
1. How would you replace all occurrences of “localhost” with “192.168.1.100” in a config file?
sed -i 's/localhost/192.168.1.100/g' config.cfg
2. What does sed -n '10,20p' file.txt do?
It prints lines 10 through 20 of file.txt.
3. How do you delete all blank lines from a file?
sed '/^$/d' file.txt
4. What’s the difference between sed 's/foo/bar/' and sed 's/foo/bar/g'?
The first replaces only the first occurrence of “foo” per line. The global flag g replaces every occurrence on each line.
5. Challenge: Write a sed command that uppercases the first letter of every word on each line.
sed 's/\b\([a-z]\)/\u\1/g' file.txt (GNU sed supports \u for uppercase in replacement)
Mini Project: Config File Updater
Create a script that safely updates server configuration values:
#!/bin/bash
# update_config.sh — Safely update config values
# Usage: ./update_config.sh config.cfg
CONFIG="$1"
BACKUP="${CONFIG}.bak"
if [ ! -f "$CONFIG" ]; then
echo "Error: Config file not found: $CONFIG"
exit 1
fi
cp "$CONFIG" "$BACKUP"
echo "Backup created: $BACKUP"
# Common config updates
sed -i 's/^server=.*/server=192.168.1.100/' "$CONFIG"
sed -i 's/^port=.*/port=9090/' "$CONFIG"
sed -i 's/^debug=.*/debug=false/' "$CONFIG"
sed -i '/^#/d' "$CONFIG"
sed -i '/^$/d' "$CONFIG"
echo "Config updated. Changes:"
diff "$BACKUP" "$CONFIG"Expected output:
Backup created: config.cfg.bak
Config updated. Changes:
9c9
< server=localhost
---
> server=192.168.1.100
10c10
< port=8080
---
> port=9090FAQ
What’s Next
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro