Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

use sed (preferrably) to replace string in a file

Posted on 2003-11-20
6
Medium Priority
?
3,339 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 7

Accepted Solution

by:
glassd earned 1200 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

[Webinar] Protection from Cyberattacks

In this session, we’ll dive into the complexities of modern cyber threats and why only multi-vector protection can keep today’s businesses secure through the various stages of a cyberattack, across multiple vectors. Thursday September 14, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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.…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.
Suggested Courses

730 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