How to Substitute Only the First Match Occurrence Using the Sed Command

The sed command, short for stream editor, is a powerful tool in Unix and Linux systems used for parsing and transforming text. One of the common tasks performed with sed is substitution, where you replace occurrences of a specific pattern within a file or a stream. However, there are scenarios where you may only want to substitute the first occurrence of a pattern. This article will guide you through the process of substituting only the first match occurrence using the sed command in detail.

In this tutorial you will learn:

  • How to create a sandbox file for testing sed commands
  • How to use sed to substitute only the first occurrence of a pattern
How to Substitute Only the First Match Occurrence Using the Sed Command
How to Substitute Only the First Match Occurrence Using the Sed Command
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Unix/Linux-based system
Software sed (Stream Editor)
Other Basic knowledge of command line operations
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

In this article, we will discuss how to substitute only the first match occurrence of a pattern using the sed command. We will first create a sandbox file to practice on, and then explore different methods to achieve our goal.

Create a Sandbox File

To begin, we need a file to test our sed commands on. Let’s create a file named sandbox.txt with the following content:

$ cat > sandbox.txt << EOL
This is a sample text file.
This file is used to demonstrate sed commands.
This line is another example line.
This is the last line.
EOL

The above code creates a file named sandbox.txt with four lines of text. We will use this file to test our sed commands.

Methods

  1. Using sed to Substitute Only the First Occurrence: To substitute only the first occurrence of the word line with LINE, we use the following command:
    $ sed '0,/line/s/line/LINE/' sandbox.txt
    

    This command works as follows:

    The range 0,/line/ tells sed to apply the substitution only between the start of the file (line 0) and the first occurrence of the word “line”. The command s/line/LINE/ is the substitution command that replaces line with LINE.

    The output of this command will be:

    sed to Substitute Only the First Occurrence
    sed to Substitute Only the First Occurrence

    As you can see, only the first occurrence of line in the third line is substituted with LINE.



  2. Using sed with a Specified Range: Another method to substitute only the first occurrence of a pattern is by using a specified range:
    $ sed '1,/line/s/line/LINE/' sandbox.txt
    

    This command works as follows:

    The range 1,/line/ tells sed to apply the substitution starting from line 1 up to the first occurrence of the word line. The command s/line/LINE/ is the substitution command that replaces line with LINE.

    The output of this command will be:

    sed with a Specified Range to Substitute Only the First Match Occurrence
    sed with a Specified Range to Substitute Only the First Match Occurrence

    Again, only the first occurrence of line is substituted with LINE.

Conclusion

Using the sed command to substitute only the first occurrence of a pattern can be very useful in text processing tasks. By creating a sandbox file and using specific sed commands, you can effectively control and perform targeted substitutions in your files. Experiment with these methods to become more proficient with sed and enhance your text processing skills.