Shashikant shah

Friday, 5 December 2014

How to replace a string in a file

#!/bin/bash

echo -n "Enter word:- "
read word

echo -n "location Search:- "
read search

E=`grep -r "$word" $search | awk -F':' '{print $1}' | uniq`
I=`grep -r "$word" $search | awk -F':' '{print $1}' | uniq | wc -l`

echo "$E" > /test/log

if [ "$I" == 0 ];then

        echo "file is not found."
else

echo -n "Enter Replace word:- "

read new

for x in `cat /test/log`
   do
        sed -i "s/$word/$new/g" $x
done
echo "***** Repalce file name logs '/test/log' ******"
echo "*****.Replace has been done.*****"

fi

No comments:

Post a Comment