Top 10 SED Use Cases Every Linux User Should Know
The Stream Editor (sed
) is one of the most powerful text-processing tools in Unix and Linux. With sed, you can automate text transformations and streamline tasks, from simple substitutions to complex multi-line edits. Here, we’ll explore the top 10 sed use cases, with practical examples to help you work efficiently from the command line.
1. Basic Text Substitution
A common use of sed
is replacing text in files. You can change all occurrences of a word, phrase, or pattern with another—useful for tasks like renaming terms in documents or code.
Example: Replace all occurrences of “cat” with “dog” in pets.txt
.
sed 's/cat/dog/g' pets.txt
In this command:
- s denotes the substitution.
cat
is the text to replace.dog
is the new text.- g ensures all occurrences on each line are replaced. Without it, only the first instance on each line changes.
Why it’s useful: Simple substitutions are essential for text file automation, especially when replacing repetitive terms across long documents or scripts.