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 :
[code language=’cpp’]
#!/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 !”
[/code]
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 :
[code language=’cpp’]
sed -e “s/$str1/$repl_str1/ig” -e “s/$str2/$repl_str2/ig” -e “s/$str3/$repl_str3/ig”
[/code]

7 thoughts on “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. 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

  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/Qword1E/Qreplacement1E/g;s/Qword2E/Qreplacement2E/g’ file1 file2 file3

  4. 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/Qword1E/Qreplacement1E/g;s/Qword2E/Qreplacement2E/g’ file1 file2 file3

Leave a Reply

Your email address will not be published. Required fields are marked *