Introduction
Sed (Stream EDitor) is a powerful text transformation tool that processes text line-by-line using a compact scripting language. It’s particularly useful for making batch edits to files and filtering text streams.
Why Learn sed?
- Perform non-interactive text editing
- Edit files without opening them
- Process large files efficiently
- Use regular expressions for pattern matching
- Automate repetitive text transformations
Basic Syntax
The basic syntax for sed is:
sed [options] 'script' input-file
sed Cheat Sheet
Option | Description |
---|
-n | Suppress automatic printing |
-i | Edit files in-place |
-e | Add multiple commands |
-r | Use extended regex syntax |
-f | Read script from file |
Command | Description |
---|
s | Substitute text |
p | Print lines |
d | Delete lines |
a | Append text after line |
i | Insert text before line |
q | Quit processing |
Sample Files Setup
Create test files for our examples:
echo "Hello World
Welcome to Linux
The quick brown fox
jumps over the lazy dog
SED is awesome
12345
Goodbye World" > poem.txt
echo "John,25,New York
Alice,30,London
Bob,22,Paris
Carol,35,Tokyo" > data.csv
Basic Examples
Example 1: Simple Substitution (s command)
sed 's/World/UNIX/' poem.txt
Hello | Planet | | | |
---|
Welcome | to | Linux | | |
The | quick | brown | fox | |
jumps | over | the | lazy | dog |
SED | is | awesome | | |
12345 | | | | |
Goodbye | Planet | | | |
Example 2: Delete Lines (d command)
Hello | Planet | |
---|
Welcome | to | Linux |
12345 | | |
Goodbye | Planet | |
Example 3: Print Specific Lines (p command)
Welcome | to | Linux | | |
---|
The | quick | brown | fox | |
jumps | over | the | lazy | dog |
Example 4: Multiple Commands (-e option)
sed -e 's/Linux/Unix/' -e '/dog/s/lazy/active/' poem.txt
Hello | Planet | | | |
---|
Welcome | to | Unix | | |
The | quick | brown | fox | |
jumps | over | the | active | dog |
SED | is | awesome | | |
12345 | | | | |
Goodbye | Planet | | | |
Example 5: In-place Editing (-i option)
cp poem.txt poem-backup.txt
sed -i 's/World/Planet/' poem.txt
cat poem.txt
Hello | Planet | | | |
---|
Welcome | to | Linux | | |
The | quick | brown | fox | |
jumps | over | the | lazy | dog |
SED | is | awesome | | |
12345 | | | | |
Goodbye | Planet | | | |
Example 6: Using Regular Expressions
Advanced Examples
Example 7: Group Replacement (& and \1)
sed -r 's/([0-9]+)/Number: \1/' poem.txt
Hello | Planet | | | |
---|
Welcome | to | Linux | | |
The | quick | brown | fox | |
jumps | over | the | lazy | dog |
SED | is | awesome | | |
Number: | 12345 | | | |
Goodbye | Planet | | | |
Example 8: Append/Insert Text (a and i commands)
sed '3a\--- Add new line after line 3 ---' poem.txt
Hello | Planet | | | | | | |
---|
Welcome | to | Linux | | | | | |
The | quick | brown | fox | | | | |
— | Add | new | line | after | line | 3 | — |
jumps | over | the | lazy | dog | | | |
SED | is | awesome | | | | | |
12345 | | | | | | | |
Goodbye | Planet | | | | | | |
sed -e '1i\<table>' \
-e 's/^/<tr><td>/' \
-e 's/,/<\/td><td>/g' \
-e 's/$/<\/td><\/tr>/' \
-e '$a\</table>' \
data.csv
<table> | |
---|
<tr><td>John</td><td>25</td><td>New | York</td></tr> |
<tr><td>Alice</td><td>30</td><td>London</td></tr> | |
<tr><td>Bob</td><td>22</td><td>Paris</td></tr> | |
<tr><td>Carol</td><td>35</td><td>Tokyo</td></tr> | |
</table> | |
Regular Expressions with sed
1. Address Ranges
sed '2,4s/o/O/g' poem.txt
Hello | Planet | | | |
---|
WelcOme | tO | Linux | | |
The | quick | brOwn | fOx | |
jumps | Over | the | lazy | dOg |
SED | is | awesome | | |
12345 | | | | |
Goodbye | Planet | | | |
2. Start/End Anchors
sed '/^SED/,/^Goodbye/d' poem.txt
Hello | Planet | | | |
---|
Welcome | to | Linux | | |
The | quick | brown | fox | |
jumps | over | the | lazy | dog |
Pro Tips
- Combine with other commands:
grep 'Linux' poem.txt | sed 's/Linux/Unix/'
- Use sed scripts for complex tasks:
echo "s/World/Planet/
s/Linux/Unix/" > script.sed
sed -f script.sed poem.txt
Hello | Planet | | | |
---|
Welcome | to | Unix | | |
The | quick | brown | fox | |
jumps | over | the | lazy | dog |
SED | is | awesome | | |
12345 | | | | |
Goodbye | Planet | | | |
- Handle whitespace carefully:
sed -E 's/\s+/ /g' poem.txt # Collapse multiple spaces
Hello | Planet | | | |
---|
Welcome | to | Linux | | |
The | quick | brown | fox | |
jumps | over | the | lazy | dog |
SED | is | awesome | | |
12345 | | | | |
Goodbye | Planet | | | |