1. Introduction

The sed command is a stream editor useful for searching, finding, and replacing, as well as text deletion and insertion. sed can also help when we want to automate text substitution across multiple occurrences of the same pattern in a file or stream without manually editing the text.

For example, we might want to use the sed command to replace the domain name in URLs within a configuration file due to a change in the application’s server location. Instead of changing the URLs manually after the pattern https://www., we’d use sed to automate the process.

In this tutorial, we’ll discuss how we can replace text after a specific word using sed.

First, we’ll briefly refresh our knowledge about the basic syntax of sed. After that, we’ll discuss replacing a text next to a specific word. We’ll also look at replacing the contents of a line after a specific word. Lastly, we’ll look at other sed options for substituting text.

2. sed Syntax

The sed command takes three arguments:

  • OPTIONS
  • SCRIPT
  • INPUT FILE

Both SCRIPT and INPUT FILE are optional parameters:

sed OPTIONS [SCRIPT] [INPUT FILE]

In this guide, we’ll use OPTIONS to modify the behavior of sed. In addition, we’ll see scripts for searching and replacing text in the SCRIPT section. Lastly, we’ll append the file name from which sed will read the text as input.

3. Sample File

To cover various text substitution methods using sed, we’ll first create a sample file. Let’s check its content using cat:

$ cat sedExample.txt
I felt happy because I saw the others were happy.
We felt happy because we saw the others were happy.

We’ll search and replace text within this sample file using sed.

4. Replace Within the Entire File

In this section, we’ll look at replacing text after a specific word using sed. First, we’ll go over multiple occurrences. After that, we’ll see how to only replace one.

4.1. Replace All Occurrences of Text

The main method to substitute text via sed is the s command.

For example, we’ll try to replace all words after the word happy from sampleSed.txt:

$ cat sedExample.txt
I felt happy because I saw the others were happy.
We felt happy because we saw the others were happy.
$ sed 's/happy.*/happy! Great!/g' sedExample.txt
I felt happy! Great!
We felt happy! Great!

Here, we used the substitution command s/happy.*/happy! Great!/g:

  • s/ indicates substitution
  • happy is the pattern we want to search for with .* matching zero or more characters after it
  • happy! Great! is the string we want to replace with
  • g stands for global and it instructs sed to replace all occurrences

In addition, we can use -i as a sed argument to perform in-place editing. In-place editing means that the command modifies the file directly instead of displaying the output on the terminal.

4.2. Replace the First Occurrence of Text

To replace only the first occurrence of text after a specific word, we’ll start from the beginning of the file and end the search at the first occurrence:

$ cat sedExample.txt
I felt happy because I saw the others were happy.
We felt happy because we saw the others were happy.
$ sed '0,/happy.*/s//happy! Great!/' sedExample.txt
I felt happy! Great!
We felt happy because we saw the others were happy.

In this case, we look from the beginning (line 0) of the text to the first occurrence of /happy.*/. Again, we perform the same substitution with /happy! Great!/. However, the extra / between s and /happy! Great!/ tells sed to search for the pattern we used for limiting the search area – /happy/. This method leaves the rest of the occurrences intact.

5. Replace Text at the Start of a Line

Now, we’ll learn how to replace text after a word from the beginning of a line.

For instance, if we want to replace text after a specific word at the beginning of a line, we’ll use ^ to match the beginning of the line and perform the substitution:

$ cat sedExample.txt
I felt happy because I saw the others were happy.
We felt happy because we saw the others were happy.
$ sed 's/^\(\s*I\s*\)felt/\1was/' sedExample.txt
I was happy because I saw the others were happy.
We felt happy because we saw the others were happy.

In the above example, we’ve used ^ to ensure we’re at the beginning of the line. There, we match against \(\s*I\s*\). In particular, we get the occurrence of the word I even if it’s surrounded by whitespace. Once we have a match, we store it in \1. Next, we expect the word to replace which is felt. The replacement pattern is was, but we also prepend the contents of capture group one (\1), i.e., the original prefix word.

6. Replace Using Word Boundary

Using a word boundary with the sed command is another method to replace text after a specific word.

A word boundary, a zero-width assertion, is a concept in regular expressions that indicates the presence of a particular word at a certain location. In other words, it is like an anchor that matches the indicates a specific position in a word. Word boundary treats alphanumeric characters and underscores as parts of words and considers other special characters and spaces as non-word characters.

For example, if we want to find the word we, we can use \b to indicate its position relative to other words:

$ cat sedExample.txt
I felt happy because I saw the others were happy.
We felt happy because we saw the others were happy.
$ sed 's/\bwe\b/you/g' sedExample.txt
I felt happy because I saw the others were happy.
We felt happy because you saw the others were happy.

In this case, we don’t replace we as part of were, but we do replace the stand-alone we. This happens because of the surrounding whitespace as part of the latter.

7. Conclusion

In this article, we discussed different ways to replace text after a specific word using the sed command.

First, we saw how to perform a replacement within an entire file. Next, we learned how to replace text after a specific word at the beginning of a line. Lastly, we looked at other sed options to replace a text after a specific pattern.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.