In this guide, we will showcase how to perform the multiple sed replacements in a single file.
Replacing Text Using Sed
Creating a Sample Text
The sed command operates a defined task on a stream. For demonstration purposes, we’re going to create a dummy file to serve as the stream. Create a text file using the following command:
the quick brown fox
jumps over the lazy dog
Check the content of the file using the following command:
To replace texts, the general command structure is as follows:
Here, sed searches for the pattern specified in the given stream and replace it with the given replacement text. The g flag ensures that sed searches the entire text for pattern matches.
Let’s try it out on our demo file. Run the following command:
Performing Multiple Replacements
In the last example, we replaced a single pattern of text. What if you wanted to replace multiple patterns?
There are a couple of different ways to do so. We can do it within a single sed command or split it into multiple ones.
Multiple Replacements in a Single Sed Command
We can pass the multiple expressions to sed using the -e flag. Have a look at the following example:
Here, the first expression replaces “the” with “hello”. The second expression replaces “lazy” with “tired”.
Instead of declaring multiple expressions, we can also combine them into a single expression, each part separated by a semicolon (;). Have a look at the following example:
If you’re running the same set of sed commands over and over again, we can place them in a single file. We can pass the file as the source of commands to sed using the -f flag. The command structure looks something like the following:
Let’s put it into action. We compiled all the sed commands that we want to run in the file commands.txt.
Note that every unique sed expression is placed in a new line for improving the visual clarity. However, you can also use the semicolon technique to merge all the expressions into a single expression.
Now, we can specify this file to sed as the list of expressions to run on the given stream:
Multiple Sed Replacements with Multiple Sed Commands
This is a more straightforward approach. Instead of using any fancy trick, we filter the stream using a sed command and pass the output to the other sed commands.
Have a look at the following example:
Here, the first sed command replaces “the” with “hello”. The output is then piped to the second sed command that replaces “lazy” with “tired”.
While this method is simple, it can become redundant very easily, especially if you need to run multiple sed expressions. In that case, using the sed command file is the most efficient way.
In this guide, we explored the various ways on how we can perform the multiple replacements using sed. We demonstrated how to pass the multiple expressions using the -e flag. We further compressed the multiple expressions into a single expression using the semicolons. For a more repetitive workload, we also demonstrated the use of a text file to run the multiple sed expressions at once.
Finally, we explored the use of sed in a simple way: piping the output of one sed command to another.
In this guide, the sed commands don’t actually change the content of the file. Learn more about the use of sed to edit the files in-place.