Using sed for Find and Replace
Modify file contents using sed
.
The sed
command, short for "stream editor," is a powerful Unix utility that enables users to parse and transform text from data streams or files. One of its most common and practical uses is performing search and replace operations across files — a task indispensable for system administrators, developers, and data analysts alike. This article explains how to use sed
for find and replace operations, covering basic syntax, practical examples, and advanced options.
Why Use sed
?
- Efficiency: Modify files directly from the command line—no need to open them in an editor.
- Automation: Integrate find and replace operations into shell scripts for bulk processing.
- Flexibility: Supports complex pattern matching and replacements using regular expressions.
Basic Syntax
The fundamental syntax for search and replace using sed
is:
sed 's/pattern/replacement/flags' filename
s
: Stands for "substitute".pattern
: The string or regular expression to search for.replacement
: The string to replacepattern
with.flags
: Modify how the substitution happens (g
for global replacement, etc.).
In-Place Editing
By default, sed
prints the result to standard output. To modify a file directly, use the -i
in-place flag:
sed -i 's/pattern/replacement/' filename
Use caution: In-place editing makes changes directly to your file.
Practical Examples
1. Replace the First Occurrence in Each Line
To replace the first occurrence of "cat" with "dog" in each line:
sed 's/cat/dog/' animals.txt
2. Replace All Occurrences in Each Line
Add the g
flag to replace all occurrences per line:
sed 's/cat/dog/g' animals.txt
3. In-Place File Replacement
Modify the file directly:
sed -i 's/cat/dog/g' animals.txt
Add .bak
to create a backup before overwriting:
sed -i.bak 's/cat/dog/g' animals.txt
4. Case-Insensitive Replacement
Use the I
flag (note: GNU sed supports this):
sed 's/cat/dog/gI' animals.txt
Using Regular Expressions
sed
supports regular expressions for advanced matches. For example, replace digits with "#" symbol:
sed 's/[0-9]/#/g' file.txt
Escaping Special Characters
If your pattern or replacement contains slashes /
, you may use a different delimiter:
sed 's|/usr/bin|/usr/local/bin|g' file.txt
Find and Replace Across Multiple Files
Use sed
with find
or xargs
to process multiple files:
find . -type f -name "*.txt" -exec sed -i 's/cat/dog/g' {} +
Tips and Best Practices
- Back Up First: Use the backup option when editing files in place.
- Test Before Modifying: Run without
-i
to preview the changes. - Escape Characters: Use
\
to escape special characters in patterns or replacements.
Conclusion
sed
is an indispensable tool for quick and effective find and replace tasks on the command line. Whether making batch changes across multiple files or performing surgical substitutions, mastering sed
increases your efficiency and control over text processing.
Further Reading:
- GNU sed Manual
man sed
command in your terminal
Unleash the full potential of your text processing workflow using sed
!