Solved

use sed (preferrably) to replace string in a file

Posted on 2003-11-20
6
3,287 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now