Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3345
  • Last Modified:

use sed (preferrably) to replace string in a file

I should edit httpd.conf file with a sh script.
I think the sed would do the job.

The scenario is
1. I have the replace string to put in a variable $NAMING_SPACE, which will be like:
    corbaloc:iiop:www.dot.com:9922/NameService. This value comes (or anything else) as input from user.
2. I need to change the old Corba namespace from the conf file, see below:

<IfModule mod_cbroker.c>
  CbrokerLocation /cbroker
  CbrokerORBArgs -ORBInitRef NameService=corbaloc:iiop:itcmdb.uta.fi:52321/NameService
</IfModule>

So I began to think how to do it? Well perhaps if I read the current value from the httpd.conf file and store it to
variable $OLD_NAMING_SPACE. Ok so far. But how?
I am sure that this is not difficult and someone can help me.  

Cheers,
jm60697
0
jm60697
Asked:
jm60697
1 Solution
 
willy134Commented:
the command is

sed -e "s/oldword/newword/" filename.txt

or
make sure you escape your slash
you can do that with assuming tcshell I suppose you will know how to convert this to bash

setenv $NAMINGSPACEFIX `echo $NAMING_SPACE | sed -e "s/\//\\\//g;"`

note the quote before the echo and at the end of the command are back ticks not apostrophes.  (usually with the tilde key)

the g makes sure that is picks up more than one file

so the end would look liek

setenv $NAMINGSPACEFIX `echo $NAMING_SPACE | sed -e "s/\//\\\//g;"`
sed -e "s/oldword/$NAMINGSPACEFIX/" filename.txt


this will replace the oldword with the naming space fix
0
 
durindilCommented:
You have to use sed with the "s" and "g" to replace ALL occurrences in a file.  Also, if you use single quotes, you do not have to specity the "-e" flag.

For example:      sed 's/oldword/newword/g' filename.txt
0
 
willy134Commented:
g is actually all the occurances on one line which ensures all occurances in a file.

all the commands are similar to the vi commands for search and replace.

I didn't know about the single quote.  Thanks
Willy
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
glassdCommented:
A tip with sed:

sed -e "s/\//\\\//g;"`

You don't HAVE to use the "/" character as a delimiter. You can use any character you like. Use something else and avoid all the escaping, for example using & as the delimiter:

sed -e "s&/&\\/&g"

(you still have to escape the escape)
0
 
willy134Commented:
If you use ampersands you won't have to escape the / in the $NAMING_SPACE variable.  Good call glassd

Also note that single quotes will change all your variables (eg $NAMING_SPACE) to the literal with dollar sign and all.  I did try and my sed allows for double quotes to do the in line action and it seems to work just fine.  I am playing like this


>sed "s&hi&$PATH&;"
hi

/home/willy134/tools/bin:/usr/bin:/usr/local/bin and so on


 
0
 
jm60697Author Commented:
Thank you all, wroking fine now =)

Cheers,
jm60697
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Tackle projects and never again get stuck behind a technical roadblock.
Join Now