find and replace query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace query
# 1  
Old 05-26-2009
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..
# 2  
Old 05-26-2009
Code:
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

-Devaraj Takhellambam
# 3  
Old 05-26-2009
Quote:
Originally Posted by devtakh
Code:
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

-Devaraj Takhellambam
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
# 4  
Old 05-26-2009
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
# 5  
Old 05-26-2009
Quote:
Originally Posted by BubbaJoe
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.
thanks for help..

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.

thanksSmilie
# 6  
Old 05-26-2009
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
# 7  
Old 05-27-2009
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

output
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 ##

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace query by reading the file

Hi Guys, I am having below file which holds data like this file.txt name,id,flag apple,1,Y apple,2,N mango,1,Y mango,2,Y I need to read the above file and frame a query like this hive -s -e "create apple_view as select 1 from main_table;" hive -s -e "create mango_view as select... (11 Replies)
Discussion started by: rohit_shinez
11 Replies

2. Shell Programming and Scripting

Find file between timestamps Query

On my linux box, I have a file say dump.txt. I then need to move to another seperte folder and need to find only one file with extension *.tar that has the closest timestamp after / next to the timestamp of the dump.txt. (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

How to use regexp to find an ipaddress from a query string?

I need help with a regexp to find out the ip address which can possibly be present in a URL. The URLs can be in any of the following form <domain>?a=12345&d=somestring1 <domain>?c=10.10.10.100&d=somestring1 <domain>?a=12345&b=somestring1&c=10.1.2.4d=somestring2... (3 Replies)
Discussion started by: ampak
3 Replies

4. Shell Programming and Scripting

Find query

when i search for a file using find i want to be able to cd to the directory of the file simple find i'm using is find . -name <filename> the output is the full path to the file, i want to use the output to cd into the directory... I need to add this into a script Any ideas pls (2 Replies)
Discussion started by: duckeggs01
2 Replies

5. UNIX for Dummies Questions & Answers

Replace query

I have a file with 3 columns as below column1 column2 column3 1, corporate,unix, 2300 2, business,unix, 23000 I need to replace "," with "|" as delimiter but the problem is the second column already has comma which is also getting replaced. I need... (4 Replies)
Discussion started by: praviper
4 Replies

6. Shell Programming and Scripting

Tables to query to find users for database from shell script

I am coding shell script. I need to connect to different databases like DB2, Oracle and Sybase. I would then need to query tables where it has all the groups, users for that database. I would also need who has what kind of permissions. EG: I know for DB2 some TABAUTH table needs to be... (0 Replies)
Discussion started by: pinnacle
0 Replies

7. Shell Programming and Scripting

to find whether update query is successfull or not using Ksh Script

i have a script that performes an update operation. I just wanted to know whether that update statement is successfull or not. Below the script: #!/bin/ksh . $HOME/conf/systemProperties/EnvSetup.properties sqlplus -silent sie/da@edn.world <<END set pagesize 0 feedback off verify off... (3 Replies)
Discussion started by: ali560045
3 Replies

8. UNIX for Dummies Questions & Answers

find -mtime query

Hello everyone, I have got two queries: 1) I want to do some work on files that were last modified yesterday. Will find ... -mtime -2 be correct or -mtime-1? 2)What about finding files that were modified today? Will it be -mtime -0 or -mtime -1? Thanks. (1 Reply)
Discussion started by: Rajat
1 Replies

9. UNIX for Advanced & Expert Users

query about find and -exec

Hi, i have query about "find" command. Do I need to put the command after -exec in single quotes? Why? For ex. see output of these three find commands. Any explanations? cheers, -Ashish (2 Replies)
Discussion started by: shriashishpatil
2 Replies

10. UNIX for Dummies Questions & Answers

find -perm query

I was going through a find tutorial and just couldn't get it...can someone explain it like he/she would explain a brain damaged dodo? "find allows you to specify a pattern that can be bit-wise ANDed with the permissions of the file. Simply put a minus sign before the octal value. The group write... (1 Reply)
Discussion started by: napolayan
1 Replies
Login or Register to Ask a Question