• Home
  • Free Stuff
  • About
  • Contact

Bash Script for Replace Multiple Words in a Multiple File

January 7, 2010

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]

BashSUSE Familytips
Share

Linux

Masim "Vavai" Sugianto
Traveller, Open Source Enthusiast & Book Lover. Works as Independent Worker & Self-Employer.

4 Comments


Diego E. "Flameeyes" Pettenò
January 7, 2010 at 17:52
Reply

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



Vavai
January 7, 2010 at 19:04
Reply

Hi Diego,

Thanks for the suggestion.



Pascal Bleser
January 7, 2010 at 22:56
Reply

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



Masim "Vavai" Sugianto
January 15, 2010 at 11:38
Reply

@Pascal,

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



Leave a Reply to Masim "Vavai" Sugianto Cancel reply

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

  • Recent Posts

    • Life goes On : Farming
    • Pursuing FIRE in Indonesia : Are You Prepared For A Recession?
    • VirtualBox Error Kernel Driver not Installed (rc=-1908) on Zorin OS 15.2
    • ZorinOS on Intel NUC Hades Canyon Series NUC817HVK
    • Banana Farm
    • Zorin OS 15.2 Ultimate
    • Haraka and LDAP Authentication with Zimbra
    • Market Crash
    • Yoga Studio
    • Portfolio Update : February 2020



© Copyright LetsBlog Theme Demo - Theme by ThemeGoods