Script: Global Search and Replace

#!/bin/sh

#set -x

N=`basename $0`

if [ $#  -ne 3 ]

then

echo “$N: Usage”

echo ”        $N <Search Path> <Search String> <Replace String> “

exit 0

fi

dir_path=$1; str_search=$2; str_replace=$3

for file in $(find $dir_path -type f)

do

sed “s/$str_search/$str_replace/g” $file >/tmp/temp$$

diff $file /tmp/temp$$ >/dev/null

if [ “$?” -ne “0” ]

then

echo “$file is replaced”

cp /tmp/temp$$ $file

fi

rm -rf /tmp/temp$$

done

 

Thanks,
Ravi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: