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
sed -e ‘s/abc/get/g’ -e ‘s/def/get/g’ filename
Latest posts by Surendra Anne (see all)
- Online training on Linux Bash shell scripting - February 8, 2018
- 15 iostat command examples in Linux/Unix - December 4, 2017
- How to Installing and Configuring Firewalld on Linux - November 29, 2017
- 4 sleep command examples in Linux/Unix - November 2, 2017
- 5 SSH alias examples in Linux - October 16, 2017