Ever wondered how to send multiple sed replacements on Linux OS? We’re here to help figure it all out.
We’re here to discuss the stream editor (sed) command in Linux. This command can be used to perform various types of operations on streams, such as search, edit, replace, and delete.
However, the most widely used operation on sed is the search and replace texts that can be used to edit a document without actually opening it on a text editor. In this article, we discuss how this command line tool can be used to send multiple sed replacements in Linux.
How to Send Multiple Sed Replacements on Linux?
Like we said earlier, we will go through all the possible steps on how to send multiple sed replacements to perform different stream operations. But first, let’s take a look at the process involved, along with the basic commands and terms.
Basic usage of sed command
The basic usage of the sed command is as follows:
$ sed's/<search_pattern>/<replacement>/g'<stream>
Here, you will specify the search pattern and the text to be replaced. The flag g
specifies that searching will be performed over the whole stream.
Specifying a separate command file for sed
There is a slight variation of this command. Instead of passing the same sed command multiple times, we can use a file to store the command and then pass the name of the file as an argument to the sed command as follows:
$ sed -f<sed_command_file><stream>
Sending multiple sed replacements to a file
We will now discuss how we can send multiple sed replacements to a file. We will discuss the whole procedure in small steps.
Creating a separate file
In the first step, we will create a text file using the cat command. The following command creates a text file and allows the user to provide sample text as input during file creation:
cat <<EOF >file.txt
When you execute the above command, it will ask the user for sample text as input for the file. Provide the content for the file. A file with the name file.txt will be created. You can check that the content you provided is available in the file via the cat command as follows:
cat file.txt
Using sed to replace texts
Now, we can use the sed command to replace texts in the file. Let’s replace all the text containing India with Bharat as follows:
$ sed's/India /Bharat /g'file.txt
Now, we will discuss how we can perform multiple replacements i.e., if we want to replace various patterns with different texts. Two approaches can be used to perform multiple replacements. Let’s discuss them one by one.
Using a single sed command for multiple replacements
Using the -e
flag, we can pass multiple expressions. The following command illustrates that:
$ sed -e's/India /Bharat /g' -e's/England /UK/'file/txt
In the above command, the text India in file.txt is replaced with Bharat, and the text England in file.txt is replaced with UK.
A slightly different way to achieve the same effect is to provide a single expression instead of multiple expressions separated by a semicolon (;). The following command illustrates that:
$ sed -e's/India /Bharat /g;s/England/UK/'file.text
Here, the two search patterns are separated by a semicolon, and the -e
flag is not used.
Using a separate command file
We can use a separate command file to store commands for sed
and pass them as arguments to sed
. For instance, the following commands can be stored in a file command.txt:
s/India /Bharat/g
s/England /UK/
In the above command file, each search expression has been placed into a separate line. However, this is not mandatory, and one can place them in a single line separated by a semicolon. Now, we can pass the command file to sed
as follows:
$ sed -f commands.txt file.txt
Here, the -f
flag specifies the file’s name containing the commands. The above command will search for India, replace it with Bharat, and replace England with the UK, as previously done.
Using multiple commands for multiple sed replacements
This is a very simple approach. We first perform the first command and filter the stream and pass it as input to another command. The following command illustrates that:
$ sed's/India /Bharat /g'test.txt | sed's/England /UK /'
The limitation of the above command is that when there are a large number of sed replacements to be made, this requires specifying all of them. In such a case, the best approach is to use the command file discussed above.
Sending the output to a file
Once all the substitutions are done, you can finally send the output to a file using the redirection operator. The following command illustrates that:
$ sed's/India /Bharat /g'test.txt | sed's/England /UK/'> output.txt
The above command outputs the content to the file output.txt after the replacements are performed.
Tip: The default sed
command doesn’t actually change the content of the original file. However, there is an option –i
that can be used to edit a file in place.
That’s all we have to showcase about the sed command and how it is used in Linux OS. It discusses how multiple sed replacements can be performed. There are two such approaches. One way is to use a single command, and the other is to use multiple sed commands separated by ppe
(|). We also discuss how commands can be stored in a file and can be specified as arguments to sed commands. More information about the sed command can be seen in the Linux manual. All you need to do is type man sed
for the manual and find more details enclosed.
If this guide helped you, please share it.