1Basic Substitution
Replace first occurrence of 'hello' with 'hi'
echo "hello world hello" | sed 's/hello/hi/'
hi world hello
2Global Substitution
Replace all occurrences of 'hello' with 'hi'
echo "hello world hello" | sed 's/hello/hi/g'
hi world hi
3Case-Insensitive Match
Replace 'hello' regardless of case
echo "Hello hello HELLO" | sed 's/hello/hi/gi'
hi hi hi
4Delete Matching Lines
Delete lines containing 'apple'
printf "apple\nbanana\ncherry\napple\n" | sed '/apple/d'
banana
cherry
5Delete Empty Lines
Remove all empty lines from text
printf "line1\n\nline2\n\nline3\n" | sed '/^$/d'
line1
line2
line3
6Print Specific Line
Print only the second line
printf "first\nsecond\nthird\n" | sed -n '2p'
second
7Print Line Range
Print lines 2 to 4
printf "1\n2\n3\n4\n5\n" | sed -n '2,4p'
2
3
4
8Print From Line to End
Print from line 3 to end of file
printf "1\n2\n3\n4\n5\n" | sed -n '3,$p'
3
4
5
9Add Text Before Lines
Add 'Fruit: ' before each line
printf "apple\nbanana\ncherry\n" | sed 's/^/Fruit: /'
Fruit: apple
Fruit: banana
Fruit: cherry
10Add Text After Lines
Add ' - delicious!' after each line
printf "apple\nbanana\ncherry\n" | sed 's/$/ - delicious!/'
apple - delicious!
banana - delicious!
cherry - delicious!
11Different Delimiter
Use '|' as delimiter for paths
echo "/usr/local/bin" | sed 's|/usr/local|/opt|'
/opt/bin
12Multiple Substitutions
Perform multiple replacements
echo "abc123def" | sed -e 's/abc/XYZ/' -e 's/def/UVW/'
XYZ123UVW
13Reference Matched Pattern
Use '&' to reference the matched pattern
echo "hello" | sed 's/hello/& world/'
hello world
14Capture Groups
Use capture groups and backreferences
echo "John Doe" | sed 's/\(.*\) \(.*\)/\2, \1/'
Doe, John
15Insert Line Before Match
Insert text before matching line
printf "apple\nbanana\ncherry\n" | sed '/banana/i\---FRUITS---'
apple
---FRUITS---
banana
cherry
16Append Line After Match
Append text after matching line
printf "apple\nbanana\ncherry\n" | sed '/banana/a\---FRUITS---'
apple
banana
---FRUITS---
cherry
17Change Matching Line
Replace entire matching line
printf "apple\nbanana\ncherry\n" | sed '/banana/c\ORANGE'
apple
ORANGE
cherry
18Print Line Numbers
Print line numbers with content
printf "apple\nbanana\ncherry\n" | sed '='
1
apple
2
banana
3
cherry
19Print Only Modified Lines
Only print lines that were modified
printf "apple\nbanana\ncherry\n" | sed -n 's/banana/ORANGE/p'
ORANGE
20Delete From Pattern to End
Delete from matching line to end of file
printf "keep\nremove\nremove\nremove\n" | sed '/remove/,$d'
keep
21Replace on Specific Line
Replace only on line 2
printf "first\nsecond\nthird\n" | sed '2s/.*/REPLACED/'
first
REPLACED
third
22Replace Except Specific Line
Replace on all lines except line 2
printf "apple\nbanana\ncherry\n" | sed '2!s/.*/FRUIT/'
FRUIT
banana
FRUIT
23Duplicate Lines
Duplicate each line using hold space
printf "hello\nworld\n" | sed 'p'
hello
hello
world
world
24Remove Trailing Whitespace
Remove trailing spaces from lines
printf "hello \nworld \n" | sed 's/[[:space:]]*$//'
hello
world
25Remove HTML Tags
Strip HTML tags from text
echo "
Hello World
" | sed 's/<[^>]*>//g'
Hello World