Solved

use sed (preferrably) to replace string in a file

Posted on 2003-11-20
6
3,332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

729 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