Managing configuration files, parsing logs, and modifying scripts are daily tasks for any system administrator. Opening each file in an editor, hunting for the right line, and manually changing it? That's time-consuming and inefficient. That's exactly why sed exists — a stream editor that lets you modify text directly from the command line.
What is sed and Why You Need It
sed stands for "stream editor" — a utility for processing text in Unix-like systems. It works like a pipeline: takes text as input, processes it according to specified rules, and outputs the result.
Common tasks sed handles:
- Bulk text replacement across files
- Deleting or adding lines based on conditions
- Extracting specific fragments from logs
- Automating configuration file modifications
- Processing data in shell scripts
The main advantage? Speed and the ability to process multiple files with a single command.
Basic Syntax
sed [options] 'command' file
The most frequently used option is -i (in-place), which saves changes directly to the source file. Without it, sed simply outputs results to the console without modifying the original.
Text Replacement — sed's Primary Function
Simple Replacement
The basic construction looks like this:
sed 's/old_text/new_text/' file.txt
Example: replace the first occurrence of "error" with "warning" in each line:
sed 's/error/warning/' server.log
⚠️ Important: by default, sed replaces only the first match in each line.
Replace All Occurrences in a Line
Add the g (global) flag at the end of the command:
sed 's/error/warning/g' server.log
Now all instances of "error" in each line will be replaced, not just the first one.
Replace and Save to File
sed -i 's/error/warning/g' server.log
The -i option overwrites the source file. For safety, you can create a backup:
sed -i.bak 's/error/warning/g' server.log
The original file will be saved as server.log.bak.
Case-Insensitive Replacement
Add the I flag:
sed 's/error/warning/gI' server.log
This will replace "error", "Error", "ERROR", and any other variations.
Working with Specific Lines
Replace in a Specific Line
Specify the line number before the command:
sed '5s/old/new/' file.txt
The replacement will only occur on line 5.
Replace in a Range of Lines
sed '10,20s/old/new/g' file.txt
This processes lines 10 through 20 inclusive.
Replace from a Line to End of File
sed '100,$s/old/new/g' file.txt
The $ symbol represents the last line of the file.
Deleting Lines
Delete a Specific Line
sed '3d' file.txt
Deletes the third line.
Delete a Range of Lines
sed '5,10d' file.txt
Delete Lines by Pattern
sed '/error/d' server.log
Removes all lines containing the word "error".
Delete Empty Lines
sed '/^$/d' file.txt
The regular expression ^$ denotes an empty line (line start immediately followed by line end).
Delete Comment Lines
sed '/^#/d' config.conf
Removes all lines starting with the # symbol.
Adding Text
Insert Line After a Specific One
sed '/pattern/a New line of text' file.txt
The a (append) command adds text after the matching line.
Example: add a comment after a directive in a config file:
sed '/ServerName/a # Domain configuration' httpd.conf
Insert Line Before a Specific One
sed '/pattern/i New line of text' file.txt
The i (insert) command adds text before the line.
Add Text to Beginning of File
sed '1i First line' file.txt
Add Text to End of File
sed '$a Last line' file.txt
Practical Examples
Change IP Address in Configuration File
sed -i 's/192.168.1.100/10.0.0.50/g' /etc/nginx/sites-available/default
Replace Directory Path
sed -i 's|/var/www/html|/home/user/public_html|g' config.php
💡 Tip: When your text contains many slashes, you can use a different delimiter — for example, | or #. This eliminates the need to escape every slash.
Comment Out a Line
sed -i '/DirectoryIndex/s/^/#/' apache.conf
Adds # at the beginning of the line containing "DirectoryIndex".
Uncomment a Line
sed -i '/DirectoryIndex/s/^#//' apache.conf
Removes # from the beginning of the line.
Extract Email Addresses from File
sed -n 's/.*\([a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]*\.[a-zA-Z]\{2,\}\).*/\1/p' contacts.txt
The -n option disables automatic output of all lines, while the p flag prints only processed lines.
Remove Leading and Trailing Whitespace
sed 's/^[ \t]*//;s/[ \t]*$//' file.txt
Two commands in one line, separated by a semicolon.
Replace Multiple Spaces with Single Space
sed 's/ */ /g' file.txt
Add Line to Specific Location in Config
For example, add a parameter after the [mysqld] section:
sed -i '/\[mysqld\]/a max_connections = 500' /etc/mysql/my.cnf
Process Multiple Files at Once
sed -i 's/old_domain.com/new_domain.com/g' /etc/nginx/sites-available/*
Delete Lines Between Two Patterns
sed '/START/,/END/d' file.txt
Removes all lines from "START" to "END" inclusive.
Working with Regular Expressions
sed supports regular expressions, which makes it even more powerful.
Replace Digits
sed 's/[0-9]/X/g' file.txt
Replaces all digits with "X".
Remove HTML Tags
sed 's/<[^>]*>//g' page.html
Extract Part of a String
Use capture groups with \( and \):
sed -n 's/.*IP: \([0-9.]*\).*/\1/p' access.log
Outputs only IP addresses from lines like "Connection from IP: 192.168.1.1".
Replace Using Captured Text
sed 's/\(error\)/[\1]/g' server.log
Wraps the word "error" in square brackets: [error].
Useful sed Options
|
Option |
Description |
|
-n |
Suppresses automatic output of lines |
|
-e |
Allows execution of multiple commands |
|
-i |
Edits file in-place |
|
-r or -E |
Enables extended regular expressions |
|
-f |
Reads commands from a file |
Example Using Multiple Commands
sed -e 's/error/warning/g' -e 's/critical/important/g' server.log
Or using semicolons:
sed 's/error/warning/g; s/critical/important/g' server.log
Reading Commands from File
Create a file commands.sed:
s/error/warning/g
s/critical/important/g
/debug/d
Apply it:
sed -f commands.sed server.log
Combining sed with Other Utilities
With grep for Pre-filtering
grep "error" server.log | sed 's/error/warning/g'
With find for Processing Multiple Files
find /var/www -name "*.php" -exec sed -i 's/mysql_/mysqli_/g' {} \;
In a Pipeline with Other Commands
cat access.log | sed 's/^.*GET //' | sed 's/ HTTP.*//' | sort | uniq -c
This chain extracts all requested URLs from the log and shows access statistics.
Common Mistakes and Solutions
Problem: replacement doesn't work because the text contains special characters.
Solution: escape them with a backslash or use a different delimiter:
Instead of this:
sed 's/http://example.com/https://example.com/g'
Do this:
sed 's|http://example.com|https://example.com|g'
Problem: sed throws an "invalid reference" error during replacement.
Solution: check whether you're using \1, \2 without corresponding capture groups \( and \).
Problem: changes aren't saved to the file.
Solution: add the -i option:
sed -i 's/old/new/g' file.txt
When sed Isn't the Right Tool
Despite its power, sed isn't a universal solution:
- For complex logic and conditionals,
awkor scripting languages work better - For binary files, you need specialized utilities
- For large-scale code refactoring, IDE find-and-replace features are more convenient
But for quick config edits, log processing, and automating routine tasks — sed is irreplaceable.
Conclusion
sed is a Swiss Army knife for text manipulation from the command line. Master the basic commands for replacement, deletion, and insertion, and you'll complete tasks in seconds that used to take minutes of manual work.
Start with simple operations — replacing text in a single file, gradually building up to more complex commands. Over time, sed will become a natural part of your workflow, especially when writing automation scripts.
Golden rule: before using -i on important files, always make a backup or test the result without this option first. One typo in a regular expression can lead to unexpected consequences.