If you’ve ever worked with Linux, chances are you’ve come across the sed command. It’s one of the most powerful tools for text manipulation, yet many beginners don’t fully explore its potential.
In this guide, we’ll cover how to use sed for replacing values in strings, variables, and files. We’ll walk through practical, real-world examples, explain each flag in plain language, and show how to avoid common pitfalls. By the end, you’ll be confident in using sed like a pro.
What is sed in Linux?
sed stands for stream editor. It allows you to search, find, and replace text in files or streams without opening an editor like vim or nano.
The basic syntax for substitution looks like this:
sed 's|pattern|replacement|'
s → substitute
pattern → the text you want to find
replacement → the text you want to replace it withSimple Example: Replace “Hello” with “Goodbye”
Simple Example: Replace “Hello” with “Goodbye”
Let’s start with a simple string replacement.
echo "Hello World" | sed 's/Hello/Goodbye/'
Output:
Goodbye World
Here, sed found “Hello” and replaced it with “Goodbye”.
Using Different Delimiters
You’re not limited to forward slashes (/). Sometimes file paths or special characters make / messy. Instead, you can use other delimiters like |.
echo "Hello World" | sed 's|Hello|Goodbye|'
Output:
Goodbye World
This is especially handy when working with URLs or file paths:
echo "/home/user/docs" | sed 's|/home/user|/data|'
Output:
/data/docs
Replacing Text in a File
Now, let’s move from strings to files. Suppose example.txt contains:
Hello World
If we run:
sed 's|Hello|Goodbye|' example.txt
The output will be:
Goodbye World
But notice — the file itself is unchanged. The modification is shown only in the terminal output.
Making Changes Permanent with -i
To actually update the file, use the -i (in-place) flag:
sed -i 's|Hello|Goodbye|' example.txt
Now, if we check the file:
cat example.txt
We’ll see:
Goodbye World
⚠️ Caution: Using -i modifies the file permanently, so make a backup if needed.
Preserving File Ownership with --copy
Sometimes, when sed -i is used, the file ownership or permissions may change. For example, the owner could become nobody.
ls -l example.txt
-rw-rw-r--. 1 nobody users 224 May 1 21:40 example.txt
To prevent this, use the --copy option:
sed --copy -i 's|Hello|Goodbye|' example.txt
This ensures file ownership remains intact.
Using sed with Variables
You can also replace text inside a variable.
foo="Hello World"
foo=$( sed "s|Hello|Hi|" <<< "$foo" )
echo $foo
Output:
Hi World
Another way
foo="Hello World"
foo=$( echo "$foo" | sed "s|Hello|Goodbye|" )
echo $foo
Output:
Goodbye World
This is extremely useful in bash scripts where dynamic text replacement is required.
Replacing Multiple Occurrences
By default, sed replaces only the first occurrence per line.
foo="Hello World
Hello World
Hello World"
echo "$foo" | sed 's/Hello/Goodbye/'
Output:
Goodbye World
Hello World
Hello World
Only the first “Hello” was replaced.
Ta replace all occurrences, use the g (global) flag:
echo "$foo" | sed 's/Hello/Goodbye/g'
Output:
Goodbye World
Goodbye World
Goodbye World
Replacing Multiple Patterns in One Command
You can chain multiple substitutions together using ;:
echo "Hello World" | sed 's|Hello|Hi|; s|World|Earth|'
Output:
Hi Earth
This is efficient when dealing with complex replacements in scripts.
Using Regular Expressions with sed
One of the most powerful features of sed is its ability to handle regular expressions (regex).
Let’s say we only want to replace “Hello” at the beginning of a line.
File: example.txt
Hello World
World Hello
Command:
sed 's|^Hello|Goodbye|g' example.txt
Output:
Goodbye World
World Hello
Here, ^Hello ensures only lines starting with “Hello” are replaced.
Real-World Practical Examples of sed
Now let’s explore how sed is used in day-to-day DevOps and system administration tasks.
1. Update Configuration Files
Suppose you want to change the default port in an Nginx config file:
sed -i 's|listen 80;|listen 8080;|' /etc/nginx/sites-enabled/default
2. Bulk Replace in Logs
Change all occurrences of ERROR to WARN in a log file:
sed -i ‘s|ERROR|WARN|g’ /var/log/app.log
sed -i 's|ERROR|WARN|g' /var/log/app.log
3. Replace URLs in Code
Updating API endpoints in code:
sed -i 's|https://api.oldsite.com|https://api.newsite.com|g' app.js
4. Remove Comments from a File
sed 's|#.*||' config.txt
This strips comments (lines starting with #).
5. Extract and Modify System Information
Want to replace hostname in /etc/hosts?
sed -i 's|old-hostname|new-hostname|' /etc/hosts
Best Practices for Using sed
Always test your
sedcommand without-ifirst.Use backups before running bulk replacements:
sed -i.bak 's|Hello|Hi|g' example.txt
(Creates
example.txt.bakas a backup.)Prefer using
|as a delimiter when dealing with file paths and URLs.Use regex carefully — unexpected replacements may break configs.
Conclusion
The sed command is a must-have skill for Linux users, DevOps engineers, and system administrators. Whether you’re updating config files, modifying logs, or dynamically changing variables in scripts, sed makes text replacement fast, flexible, and reliable.
By practicing with the examples above, you’ll move from simple string replacements to powerful regex-driven transformations. Remember to test first, back up files, and use the right flags to avoid surprises.
With sed in your toolbox, you can automate and simplify countless text manipulation tasks.
