Solved

use sed (preferrably) to replace string in a file

Posted on 2003-11-20
6
3,315 Views
Last Modified: 2007-12-19
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
Comment
Question by:jm60697
6 Comments
 
LVL 5

Expert Comment

by:willy134
ID: 9788787
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
 
LVL 6

Expert Comment

by:durindil
ID: 9789524
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
 
LVL 5

Expert Comment

by:willy134
ID: 9790477
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Accepted Solution

by:
glassd earned 300 total points
ID: 9790518
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
 
LVL 5

Expert Comment

by:willy134
ID: 9790986
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
 
LVL 3

Author Comment

by:jm60697
ID: 9795687
Thank you all, wroking fine now =)

Cheers,
jm60697
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question