Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
The sed
(Stream Editor) command is a powerful Linux tool used for text processing. It allows for text substitution, deletion, insertion, and more, directly from the command line. This command is widely used in shell scripting, automation, and data manipulation tasks.
Here is a detailed, humanized, and well-explained list of the top 20 sed
commands:
1. Basic String Substitution
Replace the first occurrence of “old” with “new” in a line
echo "I love old books" | sed 's/old/new/'
Output:
I love new books
โ How it works:
s/old/new/
โs
stands for substitute. It replaces the first occurrence of “old” with “new”.
2. Replace All Occurrences in a Line
To replace every occurrence of “old” with “new”, use the g
(global) flag
echo "old books are old but valuable" | sed 's/old/new/g'
Output:
new books are new but valuable
โ How it works:
g
โ This flag tellssed
to replace all occurrences in a line instead of just the first one.
3. Replace Text in a File
Modify a file directly
sed 's/Linux/Unix/g' file.txt
โ How it works:
- This replaces “Linux” with “Unix” in every line of
file.txt
, but does not modify the actual file.
๐ก To save changes to the file:
sed -i 's/Linux/Unix/g' file.txt
โ -i
modifies the file in place.
4. Replace a Specific Line
Replace text only in line 3
sed '3s/Linux/Unix/' file.txt
โ How it works:
3s/Linux/Unix/
โ Only the third line is modified.
5. Replace Text Between Line Ranges
Change “hello” to “hi” only from lines 2 to 5
sed '2,5s/hello/hi/g' file.txt
โ How it works:
2,5s/hello/hi/g
โ Substitutes “hello” with “hi” only between lines 2 to 5.
6. Delete a Specific Line
Remove line number 3
sed '3d' file.txt
โ How it works:
3d
โ Deletes line 3 fromfile.txt
.
7. Delete a Range of Lines
Remove lines 2 to 5
sed '2,5d' file.txt
โ How it works:
2,5d
โ Deletes lines from 2 to 5.
8. Delete Empty Lines
Remove all blank lines
sed '/^$/d' file.txt
โ How it works:
/^$/d
โ Deletes lines where there is nothing (^$
represents an empty line).
9. Insert a Line Before a Specific Line
Add a line before line 3
sed '3i This is an inserted line' file.txt
โ How it works:
3i
โ Inserts the text before line 3.
10. Insert a Line After a Specific Line
Add a line after line 3
sed '3a This is an added line' file.txt
โ How it works:
3a
โ Appends the text after line 3.
11. Replace a Word Only If It Matches a Pattern
Replace “red” with “blue” only in lines containing “apple”
sed '/apple/s/red/blue/' file.txt
โ How it works:
/apple/
โ Only matches lines containing “apple”.s/red/blue/
โ Replaces “red” with “blue”.
12. Print Only Modified Lines
Show only lines where “error” is replaced with “warning”
sed -n 's/error/warning/p' file.txt
โ How it works:
-n
โ Suppresses normal output.p
โ Prints only modified lines.
13. Print Specific Lines from a File
Display only lines 2 to 4
sed -n '2,4p' file.txt
โ How it works:
-n
โ Suppresses normal output.2,4p
โ Prints lines 2 to 4.
14. Replace Multiple Words
Change both “Linux” to “Unix” and “cloud” to “server”
sed -e 's/Linux/Unix/g' -e 's/cloud/server/g' file.txt
โ How it works:
-e
allows multiple substitutions.
15. Remove Trailing Whitespaces
sed 's/[[:space:]]*$//' file.txt
โ How it works:
[[:space:]]*$
โ Removes spaces at the end of each line.
16. Replace Text Between Delimiters
Change text inside [ ]
echo "[hello]" | sed 's/\[.*\]/[hi]/'
โ How it works:
\[.*\]
โ Matches everything inside[]
.- Replaces it with
[hi]
.
17. Use a Different Delimiter
Avoid conflicts with /
by using #
sed 's#/home/user#/var/www#g' file.txt
โ How it works:
#
is used as a separator instead of/
to avoid conflicts.
18. Save Output to a New File
sed 's/Linux/Unix/g' file.txt > newfile.txt
โ How it works:
- Redirects the modified content to
newfile.txt
without changingfile.txt
.
19. Modify Only the First 5 Lines
sed '1,5s/old/new/g' file.txt
โ How it works:
1,5s/old/new/g
โ Changes only in the first 5 lines.
20. Use sed
with a Shell Script
Create a script (modify_text.sh
):
#!/bin/bash
sed -i 's/Linux/Unix/g' file.txt
โ How it works:
-i
editsfile.txt
directly.- You can now run
./modify_text.sh
to apply the changes.
Final Thoughts
sed
is like a Swiss army knife for text processing.- You can replace, delete, modify, insert, and format text efficiently.
- Combining
sed
with grep, awk, and regex makes it even more powerful.