The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 05-26-2009
lightdensity lightdensity is offline
Registered User
  
 

Join Date: May 2009
Posts: 6
find and replace query

Hello ppl,

I am writing a script which finds multiple words match and replace it with new words.
I have server.conf file which looks like
Code:
### Welcome to server ###
### Server address and port ###
 
Server=127.0.0.1 
### Replace Server=0.0.0.0 ###
 
ServerPort=0
### Replace ServerPort=1 ####
### Enable Server ##
 
Enable Server=1
 
### Replace Enable Server=0 ###
### END OF FILE ##
-----------------------------------------------------------
i have written code for it as shown below
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'`" ]
        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
But when I run my script it only replaces the first match (Server=0.0.0.0). Remaining two matches doesnt change.
I don’t know what wrong with my code.

Can anyone help me on this?
Thanks in advance

Last edited by lightdensity; 05-26-2009 at 01:14 PM..