How to Make Multiple Edits with a Single Call to sed?

The “sed” (Stream Editor) is the command in Linux that is considered for finding, replacing, inserting and deleting the text of the string. This command is mostly used for finding and replacing text in the strings. Multiple edits are useful in reducing the time for modification of the files when the user wants to display the output with little different text.

This post will demonstrate the various examples of the sed command in Linux to make multiple edits in a file.

  • Multiple Edits Using the sed Command? (Single Call to sed)
    • Example 1: Multiple Edits in a String
    • Example 2: Multiple Edits in a FIle
    • Example 3: Multiple Edits Using Multiple sed Command

Multiple Edits Using the sed Command? (single Call to sed)

Multiple edits can be performed on a string and the files using the sed command. To perform multiple edits on any string with a single call, use the following command syntax:

Syntax

sed -e 's/[text to change]/[text to replace]/g' -e 's/[text to change]/[[text to replace]]/g'...so on

The description of the above syntax is given below:

  • The “sed” to apply the multiple edits.
  • Use the “e” flag script/expression
  • Enter the available text in the “text to change” section for replacement.
  • Enter the replacing text in the “text to replace” section. 
  • The “g” in the syntax represents a global.
  • Repeat the same pattern separated by the “e” flag according to the requirement.

Example 1: Multiple Edits in a String

To apply the multiple edits on the string, use the echo statement and pass it to the sed command using the above syntax. The following command will replace the text “Multiple” with “text” and “Edits” with “Edited”: 

$ echo "sed Command Multiple Edits" | sed -e 's/Multiple/text/g' -e 's/Edits/Edited/g'

The strings have been replaced with given text “Multiple” with “text” and “Edits” with “Edited”.

Example 2: Multiple Edits in a File

Likewise, the user can apply the multiple edits through the sed command on any file with the following syntax:

$ sed -e 's/[text to change]/[text to replace]/g' -e 's/[text to change]/[[text to replace]]/g'..... <File Name>

The description of the above syntax is given below:

  • The “sed” to apply the multiple edits.
  • Use the “e” flag script/expression
  • Define “text to change” and “text to replace” in the pattern
  • Repeat the same pattern separated by the “e” flag.
  • In the end, “File Name” describes the file name to apply the sed pattern.

For instance, the following file “file.txt” is used:

$ cat file.txt

The content “file.txt” is displayed.

The text in the “file.txt” will change with the text “Henry” with “Henry-User” and “Site” with “Blog”:

$ sed -e 's/Henry/Henry-User/g' -e 's/Site/Blog/' file.txt

The text is changed as shown above.

Repeating the Same Command

If in any case, the user is applying the same sed command pattern on the files, the particular command can be stored in a file. Then, that command file can be considered to apply to target files to obtain the output. 

Let’s store the sed patterns in the command.txt file as shown below:

$ cat command.txt

The patterns are stored in the “command.txt” file.

To apply it on the target file, use the sed command with the “f” flag. After that, define the file name such as “command.txt” and the target file such as “file.txt”:

$ sed -f command.txt file.txt

The output of the file “file.txt” is replaced with text defined in the “command.txt” file.

Example 3: Multiple Edits Using Multiple sed Commands

The user can also apply multiple edits using the multiple sed commands. For this, define the sed command pattern and file name “file.txt,” then pass it to another sed command as input through pipe command:

$ sed 's/Henry/Henry-foss/g' file.txt | sed 's/itslinuxfoss/itslinuxfoss.com/'

The above command has made following changes in the file’s content, “Henry” with “Henry-User” and “Site” with “Blog.”

Conclusion

In Linux, multiple edits with a single call through the sed command can be performed on a string or a file by repeating the multiple sed patterns separated by the “e” flag. The particular sed patterns can be stored in a file, and that file can be applied using the “f” flag to the target file to replace the given text. 

This write-up has illustrated the sed command to apply multiple edits using various examples.