Linux Commands

How Do You Do Multiple Sed Replacements

The sed (stream editor) command can perform various functions on streams in UNIX/Linux. For example: searching, finding and replacing, insertion/deletion, etc. However, it’s mostly used for finding and replacing texts. The sed command allows editing files without opening them. Because it’s a command-line tool, it can also be implemented in scripts.

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:

cat << EOF > test.txt
the quick brown fox
jumps over the lazy dog
EOF

Check the content of the file using the following command:

$ cat test.txt

Replacing Text
To replace texts, the general command structure is as follows:

$ sed 's/<search_pattern>/<replacement>/g' <stream>

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:

$ sed 's/the/hello/g' test.txt

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:

$ sed -e 's/the/hello/g' -e 's/lazy/tired/' test.txt

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:

$ sed -e 's/the/hello/g;s/lazy/tired/' test.txt

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:

$ sed -f <sed_command_file> <stream>

Let’s put it into action. We compiled all the sed commands that we want to run in the file commands.txt.

$ cat 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:

$ sed -f commands.txt test.txt

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:

$ sed 's/the/hello/g' test.txt | sed 's/lazy/tired/'

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.

Conclusion

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.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.