sed Substitution Basics

LinuxBeginner
Practice Now

Introduction

A common text-processing task is "find and replace." In word processors, this is interactive. In Linux command-line environments, sed performs the same idea in a scriptable, non-interactive way.

sed is a non-interactive text editor that processes text line by line. It is particularly powerful for substitution tasks, where you need to replace specific patterns with new text. This is useful in automation because no human needs to sit in front of an editor and make the same change repeatedly.

In this lab, you will learn how to:

  1. Substitute Text (s): Perform basic find and replace operations.
  2. Global Replacement (g): Replace all occurrences of a pattern in a line, not just the first one.
  3. Use Custom Delimiters: Handle text containing slashes (like file paths) using alternative delimiters.
  4. Edit In-Place with Backups: Modify files safely by creating backup copies automatically.

Basic Substitution

Let's start with a simple substitution on a string. We will change the word "World" to "Linux".

The command for substitution in sed is s.

The syntax is s/Pattern/Replacement/.

This command shape is so common that many users think of sed as a "search and replace tool," but it is really a stream editor. Substitution is simply one of its most frequently used editing commands.

Run this command to see it in action:

echo "Hello World" | sed 's/World/Linux/'

How it works:

  1. echo sends "Hello World" to the standard output.
  2. The pipe | passes this output to sed.
  3. sed reads the text, looks for the pattern "World", replaces it with "Linux", and prints result.

Note: This command does not modify the original source permanently; it simply outputs the modified text to the screen (Standard Output).

That distinction is central to safe command-line editing. In Unix style, tools often show you a transformed stream first, and only later do you choose whether to save or redirect the result.

Global Replacement

By default, sed only replaces the first match it finds on each line.

If you have the text "apple apple" and substitute "apple" with "orange", sed will output "orange apple".

To replace all occurrences on a line, you must use the Global flag (g) at the end of the substitution command.

The important detail is "on each line." sed processes one line at a time, so the g flag affects all matches within the current line, not the whole file as one giant block of text.

Let's verify our sample file quote.txt:

cat quote.txt

Output: I love Coding. Coding is fun.

Now, try to replace "Coding" with "Programming" without the global flag:

sed 's/Coding/Programming/' quote.txt

Result: I love Programming. Coding is fun. Notice that the second instance of "Coding" was skipped.

Now add the g flag:

sed 's/Coding/Programming/g' quote.txt

Result: I love Programming. Programming is fun. Now both instances have been replaced successfully.

Use Different Delimiters

Standard sed syntax uses forward slashes / as delimiters: s/old/new/.

However, if the text you are replacing contains slashes (like a file path: /usr/local/bin), you would have to escape every slash with a backslash. This makes the command hard to read: s/\/usr\/local\/bin/\/bin/.

Fortunately, sed allows you to use almost any character as a delimiter. Common alternatives are #, :, or |.

Choosing a readable delimiter is a practical skill, not just a syntax trick. When patterns contain many / characters, changing the delimiter makes the command easier to understand and less error-prone.

Create a file containing a path:

echo "/usr/local/bin" > path.txt

Now replace /usr/local/bin with /bin using # as the separator:

sed 's#/usr/local/bin#/bin#' path.txt

Result: /bin

Because we used # as the delimiter, sed treated the / characters in the path as normal text, making the command much cleaner and easier to read.

In-Place Editing with Backup

So far, sed has only printed the changes to the screen. To modify the file directly ("in-place"), we use the -i option.

However, modifying files directly can be risky. If you make a mistake in your pattern, you might lose data. Ideally, you should create a backup of the original file before modifying it.

You can do this by providing an extension to the -i option, like -i.bak.

Let's modify quote.txt and create a backup named quote.txt.bak automatically.

sed -i.bak 's/fun/awesome/' quote.txt

Now check the content of the modified file:

cat quote.txt

Result: I love Coding. Coding is awesome.

And verify that the backup exists and contains the original text:

ls -l quote.txt.bak
cat quote.txt.bak

Result: I love Coding. Coding is fun.

This is a best practice when applying batch edits to multiple files.

That backup habit matters because non-interactive edits are fast but unforgiving. When you automate text replacement across files, a small pattern mistake can spread quickly, so keeping the original copy is a simple form of safety.

Summary

In this lab, you learned the essentials of text substitution with sed:

  1. Substitution (s///): The standard command for finding and replacing text.
  2. Global Flag (g): Replacing all instances of a pattern on a line.
  3. Custom Delimiters: Using characters like # to safely handle paths and URLs containing /.
  4. In-Place Backup (-i.bak): Safely modifying files while preserving the original content.

These skills enable you to automate complex text processing tasks efficiently and safely.