Bash Script for Replace Multiple Words in a Multiple File

I’m using the following script to replace a configuration file. As example, I’m trying to create an auto-configuration for setting up PXE boot service. YAST maybe the best tools to do what I want, but YAST need a few step to do.  I could  make a same result with a simple task by preparing default configuration file (the successful configuration on my server), ask some question and then  automatically replace some words with the answer.

You may also use the following script on hosting service :

#!/bin/bash
search_dir="/home/vavai/pxe-preconfig/"
str="vavai.com"
repl_str="vavai.net"
for file in $(grep -l -R $str $search_dir)
do
sed -e "s/$str/$repl_str/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
echo "File has been successfully modified!" $file
done
echo "Done !"

If you need to get the dynamic process, modify the script to ask some question and then replace the sed variable with the answer.

But, how if we want to replace multiple string ? Well, it should be easy to use multiple regular expression on sed parameter as below :

sed -e "s/$str1/$repl_str1/ig"  -e "s/$str2/$repl_str2/ig" -e "s/$str3/$repl_str3/ig"

Related Entries

4 Responses for “Bash Script for Replace Multiple Words in a Multiple File”

  1. On modern GNU sed (that is, from at least the last five years) – and you’re already assuming GNU sed when you use multiple -e parameters – you can use the -i parameter to do “inline replace”.

    This way you don’t have to copy the file to a temporary version and then back again for the replacement, nor you really need to grep for the string: just use sed -i -e ‘…’ -e ‘…’ /list/of/files/*.foo

  2. Vavai says:

    Hi Diego,

    Thanks for the suggestion.

  3. As Diego already said, it is a lot easier to use the “-i” in-place editing flag, because it does it properly (locking, really temporary/random filename, …). If not, it is a lot safer to use “mktemp” instead of a fixed filename.

    But be aware that those “words” are actually regular expressions, and that you cannot use e.g. “/” in the word to be replaced or the replacement.

    I would rather resort to Perl, because it has the ability to escape complete strings in regular expressions. Oh, and Perl also has the -i flag :)

    perl -pi -e ‘s/\Qword1\E/\Qreplacement1\E/g;s/\Qword2\E/\Qreplacement2\E/g’ file1 file2 file3

  4. Masim "Vavai" Sugianto says:

    @Pascal,

    Thank you. I’m not really familiar with Perl but I would love to try your code. :-)

Leave a Reply

About Vavai

Masim Vavai Sugianto Masim Vavai Sugianto, Indonesian, male, 32 years old, born and live in Bekasi-West Java, a small town near Jakarta – main city of Indonesia – since 17 May 1976. Founder of Indonesian openSUSE and Zimbra Community, an adventure, travelling and book lover.



I live in a tropical country, Indonesia that only has two seasons, dry season and rainy season. I love the dry season with bright sunshine and rare rain…There is a joke about the seasons in Indonesia. Indonesia is known as a country with so many season, ie : durian season, mango season, married season and much more...



ContactI'm currently working as an IT support for a small company based on Jakarta. My career has led me to specialize in Sysadmin, networking and software implementation with current focus on Linux and open source area. I have experience on MRP, ERP, Gemba Kaizen, Just in Time, Six Sigma and TQC/TQM. Please use my contact page if you wish to contact me.
Log in - BlogNews Theme by Gabfire themes