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.
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:
The main advantage? Speed and the ability to process multiple files with a single command.
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.
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.
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.
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.
Add the I flag:
sed 's/error/warning/gI' server.log
This will replace "error", "Error", "ERROR", and any other variations.
Specify the line number before the command:
sed '5s/old/new/' file.txt
The replacement will only occur on line 5.
sed '10,20s/old/new/g' file.txt
This processes lines 10 through 20 inclusive.
sed '100,$s/old/new/g' file.txt
The $ symbol represents the last line of the file.
sed '3d' file.txt
Deletes the third line.
sed '5,10d' file.txt
sed '/error/d' server.log
Removes all lines containing the word "error".
sed '/^$/d' file.txt
The regular expression ^$ denotes an empty line (line start immediately followed by line end).
sed '/^#/d' config.conf
Removes all lines starting with the # symbol.
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
sed '/pattern/i New line of text' file.txt
The i (insert) command adds text before the line.
sed '1i First line' file.txt
sed '$a Last line' file.txt
sed -i 's/192.168.1.100/10.0.0.50/g' /etc/nginx/sites-available/default
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.
sed -i '/DirectoryIndex/s/^/#/' apache.conf
Adds # at the beginning of the line containing "DirectoryIndex".
sed -i '/DirectoryIndex/s/^#//' apache.conf
Removes # from the beginning of the line.
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.
sed 's/^[ \t]*//;s/[ \t]*$//' file.txt
Two commands in one line, separated by a semicolon.
sed 's/ */ /g' file.txt
For example, add a parameter after the [mysqld] section:
sed -i '/\[mysqld\]/a max_connections = 500' /etc/mysql/my.cnf
sed -i 's/old_domain.com/new_domain.com/g' /etc/nginx/sites-available/*
sed '/START/,/END/d' file.txt
Removes all lines from "START" to "END" inclusive.
sed supports regular expressions, which makes it even more powerful.
sed 's/[0-9]/X/g' file.txt
Replaces all digits with "X".
sed 's/<[^>]*>//g' page.html
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".
sed 's/\(error\)/[\1]/g' server.log
Wraps the word "error" in square brackets: [error].
|
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 |
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
Create a file commands.sed:
s/error/warning/g
s/critical/important/g
/debug/d
Apply it:
sed -f commands.sed server.log
grep "error" server.log | sed 's/error/warning/g'
find /var/www -name "*.php" -exec sed -i 's/mysql_/mysqli_/g' {} \;
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.
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
Despite its power, sed isn't a universal solution:
awk or scripting languages work betterBut for quick config edits, log processing, and automating routine tasks — sed is irreplaceable.
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.