We can not do this without using sed command. Below are the ways you can do that.
Below example will try to replace abc or cde with xyz. I used regexp ‘|’ to accomplish this task. -r is for enabling regexp and -i for inserting the changes in to the file.
sed -ri ‘s/abc|cde/xyz/g’ filename
Some more examples on replacing multiple words/char. In below example we can replace either a or b with d
sed -i ‘s/[ab]/d/g’ filename
I want to replace swapon and swapoff words with just word “free”
sed -r ‘s/swap(on|off)/free/g’ filename
In order to understand () and | we have know about regexp please click here to know more about regexp
Below is bit odd way to do the multiple word changes using sed.
sed ‘s/abc/get/g;s/def/get/g’ filename
or
sed -e ‘s/abc/get/g’ -e ‘s/def/get/g’ filename
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018