Showing posts with label sed. Show all posts
Showing posts with label sed. Show all posts

Tuesday, May 19, 2009

Find and replace in text files recursive using grep and sed

I've created a small shell script for searching and replacing text in multiple files in a subdirectory.
My reason was that I needed a tool to change the ip-address of several mysql-servers in several files named config.ini, all residing in subdirectories.
The syntax is:
$ ./myfile.sh old-ip new-ip

And here is the script:


#!/bin/sh
for fl in `find ./* | grep config.ini$`;
do
(
echo $fl
sed --in-place "s/$1/$2/g" $fl
)
done


The reason for me doing this is that I'm using Amazon EC2. For a fast connection, I need to use the EC2 internal IP. When I for some reason need to change the MySQL server to be called, that internal ip is changed, and hence I need to change my config files.