Sed: Finding and Replacing Text in Bash
This post introduces the sed command in Bash, focusing on finding and replacing text. It covers basic syntax, global substitution, in-place file editing, and using sed in pipelines with practical examples suitable for beginners.
If you spend any time working in the Linux terminal, you will quickly discover that manipulating text is one of the most common tasks you will face. Whether you are editing configuration files, cleaning up log output, or transforming data in scripts, the sed command is one of the most powerful tools at your disposal. This post focuses on the most practical use case: finding and replacing text.
What Is Sed?
sed stands for Stream Editor. It reads text line by line, applies transformations you define, and outputs the result. It works on files, or on text piped from another command. Unlike opening a file in a text editor and making changes manually, sed lets you automate those changes, which makes it incredibly useful in scripts and automation workflows.
The Basic Substitution Syntax
The core syntax for find and replace in sed looks like this:
sed 's/find/replace/' filenameBreaking this down:
- s: tells
sedthis is a substitution command - find: the text you want to search for
- replace: the text you want to substitute in
- filename: the file to read from
For example, if you have a file called config.txt containing the line hostname router1 and you want to change it to hostname router2, you would run:
sed 's/router1/router2/' config.txtBy default, sed prints the result to your terminal without modifying the original file. Think of it as a preview. You will see the changed output, but config.txt remains untouched.
Making Changes Global on Each Line
By default, sed only replaces the first occurrence on each line. If a line contains the word router1 three times, only the first one gets replaced. To replace all occurrences on every line, add the g flag at the end of your expression:
sed 's/router1/router2/g' config.txtThis is almost always what you want when doing a global find and replace.
Editing Files In Place
Once you are happy with the substitution, you can write the changes directly back to the file using the -i flag (which stands for "in place"):
sed -i 's/router1/router2/g' config.txtOn macOS, the -i flag requires an extension argument. To avoid creating a backup file, pass an empty string like this: sed -i '' 's/router1/router2/g' config.txt. On Linux, the version shown above works as expected.
If you want to be cautious, you can create a backup automatically by providing an extension:
sed -i.bak 's/router1/router2/g' config.txtThis saves your original as config.txt.bak before making any changes. Highly recommended when editing important files.
Piping Output Into Sed
You do not always need to work with files directly. sed works beautifully in a pipeline. For example, to filter command output on the fly:
cat /etc/hosts | sed 's/localhost/myhost/g'Or with a more practical automation scenario, replacing a placeholder in a template before passing it to another command:
cat template.cfg | sed 's/DEVICE_NAME/switch-01/g' | ssh [email protected]A Quick Practical Example
Suppose you have an Ansible inventory file and you need to bulk rename a group of hosts from lab- to prod-. Instead of editing each entry manually, you can run:
sed -i.bak 's/lab-/prod-/g' inventory.iniIn seconds, every occurrence of lab- becomes prod-, and you have a backup just in case.
A Few Things to Watch Out For
- Special characters: If your search or replace string contains a forward slash
/, you need to escape it with a backslash, like\/, or use a different delimiter such as|or#. For example:sed 's|/etc/old|/etc/new|g' file.txt - Case sensitivity: By default,
sedis case sensitive.Router1androuter1are not the same thing. - Whitespace matters: Extra spaces around your search term will prevent a match, so be precise.
What's Next
sed is capable of much more than simple substitution, including deleting lines, inserting text, and working with line numbers. But mastering find and replace is the foundation, and it will serve you well in everyday scripting tasks. In the next post, we will look at awk, another essential text processing tool that gives you column-level control over structured data.