I am writing a script which finds multiple words match and replace it with new words.
I have server.conf file which looks like
### 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
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.
Your IFS is set wrong. THe IFS="" says there is no separator so the whole file is read. You can only use IFS is there is actually a seperator between each field. So cat $FILE will not give you line by line it will give you the while file as a field at once. Hence only matching 1 time. Since you really want look at each line as a record you should use awk or sed to find and replace the text.
So this will work for you which was posted by devtakh which says search the whole file and replace the matching pattern with the given pattern for each instance you want to change. It will find each one separately and change the file the way you want.
sed '/Server=127.0.0.1/{s//Server=0.0.0.0/g}
/ServerPort=0/{s//ServerPort=1/g}
/Enable Server=1/{s//Enable Server=0/g}
' /opt/server.conf > /opt/new_server.conf
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.
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
#!/usr/bin/env python
import fileinput
d={ "Server":"0.0.0.0", "ServerPort":"1", "Enable Server":"0" }
for line in fileinput.FileInput("file",inplace=1):
line=line.strip()
if not line.startswith("#") and line!="":
line=line.split("=")
print line[0]+"="+d[line[0]]
else:
print line
output
# ./test.py
# more file
### Welcome to server ###
### Server address and port ###
Server=0.0.0.0
### Replace Server=0.0.0.0 ###
ServerPort=1
### Replace ServerPort=1 ####
### Enable Server ##
Enable Server=0
### Replace Enable Server=0 ###
### END OF FILE ##
Your solution works fine... but I am facing new problem now... Actually I didn�t mentioned my complete input file which is
### 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 ###
Enable Agent=1
Enable ip=1
### Now I dont need to change above two options ###
### END OF FILE ##
So what i have implemented look like
FILE="/opt/server.conf"
NEW_FILE="/opt/new_server.conf"
exec 0<$FILE
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
Output file looks like
Server=0.0.0.0
ServerPort=1
Enable Server=0
So when i execute my code, output file only contains the strings which i have changed, there are certain strings which i want to copy as it .
I am still new to shell script, can you explain me as where to and how to use find . -type f | xargs sed -i "s/Server=127.0.0.1/Server=0.0.0.0/g" in my mentioned code..
thanks
-----Post Update-----
hi devtakh
Sorry mate for bothering you with so many question.. There was problem in my script and i found it.. Now output is coming very correctly...
Thanks man.. Really appreciate your response..
Thanks to all who gave their valuable suggestions.