How Do You Do Multiple Sed Replacements

0

The sed (movement editor) command can carry out more than a few purposes on streams in UNIX/Linux. For instance: looking, discovering and changing, insertion/deletion, and many others. However, it’s most commonly used for locating and changing texts. The sed command permits enhancing recordsdata with out opening them. Because it’s a command-line device, it can be carried out in scripts.

In this information, we will be able to exhibit how you can carry out the more than one sed replacements in one record.

Replacing Text Using Sed

Creating a Sample Text
The sed command operates an outlined job on a movement. For demonstration functions, we’re going to create a dummy record to function the movement. Create a textual content record the use of the next command:

cat check.txt
the fast brown fox
jumps over the lazy canine
EOF

Check the content material of the record the use of the next command:

Replacing Text
To change texts, the overall command construction is as follows:

$ sed ‘s///g’

Here, sed searches for the development specified within the given movement and change it with the given substitute textual content. The g flag guarantees that sed searches all the textual content for development suits.

Let’s check it out on our demo record. Run the next command:

$ sed ‘s/the/hi/g’ check.txt

Performing Multiple Replacements

In the closing instance, we changed a unmarried development of textual content. What should you sought after to interchange more than one patterns?

There are a few alternative ways to take action. We can do it inside a unmarried sed command or cut up it into more than one ones.

Multiple Replacements in a Single Sed Command
We can go the more than one expressions to sed the use of the -e flag. Have a have a look at the next instance:

$ sed -e ‘s/the/hi/g’ -e ‘s/lazy/drained/’ check.txt

Here, the primary expression replaces “the” with “hello”. The 2nd expression replaces “lazy” with “tired”.

Instead of stating more than one expressions, we will additionally mix them right into a unmarried expression, every phase separated via a semicolon (;). Have a have a look at the next instance:

$ sed -e ‘s/the/hi/g;s/lazy/drained/’ check.txt

If you’re operating the similar set of sed instructions again and again, we will position them in one record. We can go the record because the supply of instructions to sed the use of the -f flag. The command construction appears to be like one thing like the next:

$ sed -f

Let’s put it into motion. We compiled the entire sed instructions that we wish to run within the record instructions.txt.

Note that each and every distinctive sed expression is positioned in a brand new line for bettering the visible readability. However, you’ll be able to additionally use the semicolon method to merge the entire expressions right into a unmarried expression.

Now, we will specify this record to sed because the listing of expressions to run at the given movement:

$ sed -f instructions.txt check.txt

Multiple Sed Replacements with Multiple Sed Commands
This is a more uncomplicated manner. Instead of the use of any fancy trick, we filter out the movement the use of a sed command and go the output to the opposite sed instructions.

Have a have a look at the next instance:

$ sed ‘s/the/hi/g’ check.txt | sed ‘s/lazy/drained/’

Here, the primary sed command replaces “the” with “hello”. The output is then piped to the second one sed command that replaces “lazy” with “tired”.

While this system is modest, it may turn out to be redundant very simply, particularly if you want to run more than one sed expressions. In that case, the use of the sed command record is the most productive means.

Conclusion

In this information, we explored the more than a few techniques on how we will carry out the more than one replacements the use of sed. We demonstrated how you can go the more than one expressions the use of the -e flag. We additional compressed the more than one expressions right into a unmarried expression the use of the semicolons. For a extra repetitive workload, we additionally demonstrated using a textual content record to run the more than one sed expressions immediately.

Finally, we explored using sed in a easy means: piping the output of 1 sed command to every other.

In this information, the sed instructions don’t if truth be told alternate the content material of the record. Learn extra about using sed to edit the recordsdata in-place.

Happy computing!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More