![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tables to query to find users for database from shell script | pinnacle | Shell Programming and Scripting | 0 | 04-10-2009 05:18 PM |
| to find whether update query is successfull or not using Ksh Script | ali560045 | Shell Programming and Scripting | 3 | 01-07-2009 01:41 AM |
| find -mtime query | Rajat | UNIX for Dummies Questions & Answers | 1 | 07-09-2008 07:12 AM |
| query about find and -exec | shriashishpatil | UNIX for Advanced & Expert Users | 2 | 04-12-2007 01:16 AM |
| find -perm query | napolayan | UNIX for Dummies Questions & Answers | 1 | 11-28-2006 02:28 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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
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 12:14 PM.. |
|
||||
|
Quote:
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
thanks |
|
||||
|
Quote:
so what if i set IFS="\n\t" than i guess, cat $FILE will give line by line.. ok!! i will try it out.. meanwhile could you check my remaining code.. and tell whether my code is correct or not. thanks ![]() |
|
||||
|
Quote:
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
|
|
||||
|
if you have Python
Code:
#!/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
Code:
# ./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 ## |
| Sponsored Links | ||
|
|