Quote:
Originally Posted by lightdensity
thanks for your response..Actually i am new to shell scripting
Code:
FILE="/opt/server.conf"
NEW_FILE="/opt/new_server.conf"
IFS=""
for line in `cat ${FILE}`; do
#echo ${line}
if [ -n "`echo ${line} | grep 'Server=127.0.0.1'`" ] #grep command searches for right string
then
echo ${line} | sed 's|Server=127.0.0.1|Server=0.0.0.0|g' >>$NEW_FILE
elif [ -n "`echo ${line} | grep 'ServerPort=0'`" ]
then
echo ${line} | sed 's|ServerPort=0|ServerPort=1|g' >>$NEW_FILE
elif [ -n "`echo ${line} | grep 'Enable Server=1'`" ]
then
echo ${line} | sed 's|Enable Server=1|Enable Server=0|g' >>$NEW_FILE
else
echo ${line} >>$NEW_FILE
fi
done
So, how to find out which for the right string??
thanks
|
It will be a waste of resource to use the shell script when
sed can handle it. you do not need to check if the word exists and if it exists, then replace it.
sed can do that for you.
But incase you need to do..I suggest you use while loop, instead of a for loop with the cat command.
Code:
FILE="/opt/server.conf"
NEW_FILE="/opt/new_server.conf"
while read line
do
#echo ${line}
if [ -n "`echo ${line} | grep 'Server=127.0.0.1'`" ] #grep command searches for right string
then
echo ${line} | sed 's|Server=127.0.0.1|Server=0.0.0.0|g' >>$NEW_FILE
elif [ -n "`echo ${line} | grep 'ServerPort=0'`" ]
then
echo ${line} | sed 's|ServerPort=0|ServerPort=1|g' >>$NEW_FILE
elif [ -n "`echo ${line} | grep 'Enable Server=1'`" ]
then
echo ${line} | sed 's|Enable Server=1|Enable Server=0|g' >>$NEW_FILE
else
echo ${line} >>$NEW_FILE
fi
done < $FILE
-Devaraj Takhellambam