Ever needed to make quick edits to a text file in Linux? While text editors are great for complex changes, the sed
command offers a powerful way to manipulate text files from the command line. sed stands for “stream editor,” and it processes text files line by line.
Here’s what sed
can do:
- Search and Replace: A core function of
sed
is to find specific text patterns and replace them with something else. This is useful for tasks like updating configuration files or correcting typos across multiple files. - Insert and Delete Lines: Need to add a header to every file in a directory?
sed
can handle that. It can also delete unwanted lines based on patterns. - Filtering Lines:
sed
can extract specific lines based on patterns. This is handy for grabbing only the lines you need from a larger file.
Benefits of Using sed:
- Efficiency:
sed
excels at automating repetitive edits across multiple files. It can be much faster than opening each file in a text editor. - Precision: Using regular expressions in
sed
allows for powerful pattern matching, ensuring accurate edits. - Scripting:
sed
can be integrated into shell scripts for complex text manipulation tasks.
Getting Started with sed:
The basic syntax for sed
is:
command
is a sed instruction that tells it what to do with the file.file
is the text file you want to edit.
There are many sed
commands available, but a common one is s/pattern/replacement/
. This command searches for the pattern
and replaces it with the replacement
text.
Here’s a breakdown of the replacement syntax:
- /pattern/: This is the text pattern you want to search for. You can use regular expressions for complex matching.
- /replacement/: This is the text that will replace all occurrences of the
pattern
. - / (global flag): By default,
sed
only replaces the first occurrence of the pattern on each line. Adding theg
flag ensures that all occurrences are replaced.
For instance, the command:
sed 's/error/success/g' myfile.txt
would replace all instances of “error” with “success” in the file “myfile.txt”.
Replacement Examples with sed:
The s/pattern/replacement/
command offers flexibility for various editing tasks. Here are some examples to showcase its power:
1. Simple Replacement:
This is the most basic usage, where you directly replace a specific word. For example:
sed 's/bug/feature/g' issues.log # Replaces all "bug" with "feature" in "issues.log"
2. Case-Insensitive Replacement:
Use the i
flag to make the search case-insensitive. For instance:
sed 's/Color/COLOUR/gi' document.txt
# Replaces "Color" (and "color") with "COLOUR" (globally)
3. Replacing Only at the Beginning/End of Line:
Use ^
and $
to match the beginning and end of the line, respectively. Here’s an example:
sed 's/^# //' comments.txt
# Removes comments starting with "#" (assuming single space after)
sed 's/.$//' trimmed_file.txt
# Removes the last character from each line
4. Replacing with Backreferences:
sed allows capturing parts of the pattern for reuse in the replacement. Let’s see how:
sed 's/(\w+).txt/converted_\1.txt/' files.list
# Prepends "converted_" before filename (captures word as \1)
5. Replacing with Files:
Read replacement text from a separate file using the r
flag:
sed 's/placeholder/$(cat replacements.txt)/' config.ini
# Replaces "placeholder" with content of "replacements.txt"
Remember: The g
flag is crucial for replacing all occurrences on a line. By using these examples and exploring the sed
manual, you’ll master text manipulation on the fly!