Link to home
Start Free TrialLog in
Avatar of Dipesh Patel
Dipesh Patel

asked on

Automate script to find and replace parameter in UNIX servers

Hi Experts,
 We have some application that we are migrating from one environment to another environment. We have some tomcat , Database server Apache and batch servers for this application. We will have new host/server for this application.  Our job is to find some old server name references and replace with new one specially database server for connection string for java application in config file or any location.

We are using Putty to log on those unix servers. We don't want to manually find one by one in all the file and replace it.

Is there any way or script to find those instances and replace automatically?
Avatar of serialband
serialband
Flag of Ukraine image

for i in list of files ; do sed -i -e 's/oldServer/newServer/g' $i ; done

Open in new window

for i in *.conf ; do sed -i -e 's/oldServer/newServer/g' $i ; done

Open in new window

Tip: Embedding any hostnames in source files... will cause this same situation to recur in the future.

Instead, collect all your hostnames into a some sort of config.php or config.inc or config.yaml file where all your settings live, then include this file in every other file.

This way you only only change hostname settings in one place.
Avatar of Dipesh Patel
Dipesh Patel

ASKER

Hi David,
 We just wanted to make sure that there is no hard coded any where by mistake so planning to run through to whole server.

Hi Serialband,
 We have 5 servers(1 batch, 2 tomcat , 2 Apache). We will also need the report where we are replacing and then we will replace. I did not get chance to run those code. Can you please help me with both scripts?

Thank You
I'd likely just find the instances + change them by hand, as automatically changing them... if there's any typo in your find + replace... you can end up with mangled/corrupt site configs.

To find all occurrences, something like this will do...

find /dir -type f -exec egrep -il "$site" {} \;

Open in new window


This will provide you with a list of all files containing the $site string.
maybe a recursive sed would help, if you do not know the file list

find /dir | xargs -t -n 50 -P 10 sed -i -e 's/oldServer/newServer/g ; t ; w /dev/stderr'

that would run 10 // sed scripts that would each handle 50 files

i also added some useless output in stderr

beware that since the processing is parallel, the lines will be mixed up. this is not meant to track what changes are made in what file, but rather to simply know the script is still running and about what was processed.


--

if you want to follow exactly try one of these

find /dir | xargs -t -n 1 sed -i -e 's/oldServer/newServer/g ; t ; w /dev/stderr'

or possibly

find /dir | xargs -t -n 1 sh -c "sed -i -e 's/oldServer/newServer/g ; t ; w /dev/stderr' $1 | sed \"s/^/$1: /\" "

the first one simply removes parallel processing, the second will prefix each line with the file name. both will be slower than the original.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.