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.

Sed: Finding and Replacing Text in Bash

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 best SSH/Telnet client I've ever used: If you're doing serious CLI work daily, SecureCRT is the best terminal client I've come across in 20+ years of networking. I don't currently have a licence because I'm not doing enough console work to justify the cost — but the moment that changes, it's the first thing I'd buy. PuTTY is free and gets the job done, but SecureCRT is in a different league.

The core syntax for find and replace in sed looks like this:

sed 's/find/replace/' filename

Breaking this down:

  • s: tells sed this 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.txt

By 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.txt

This 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.txt

On 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.txt

This 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.ini

In 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, sed is case sensitive. Router1 and router1 are 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.

🔧
For bulk text manipulation in configuration files or automation scripts, sed combined with grep and awk gives you a powerful trio — master these three and you can handle almost any text transformation task directly from the terminal. sed, awk and grep.
🔧
If you are regularly pushing configuration changes to network devices over SSH, SecureCRT offers scripting support and session management that pairs well with shell-based automation workflows like the ones shown here. SecureCRT, OpenSSH and Ansible.