LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-24-2005, 11:15 PM   #1
dave4545
Member
 
Registered: Oct 2002
Distribution: Redhat Linux 7.0 Apache/1.3.27 PHP Version 4.3.1 server api Apache
Posts: 46

Rep: Reputation: 15
How can I replace this string with another using sed?


Hey. I searched for answer before asking.
I couldn't seem to find anything that would help.
How can I use the search and replace feature using sed?
I need to replace some text on about 100 pages.

I wanted to find this
<!--#exec cgi="/cgi-bin/helper/adoption.pl" -->

and replace it with

<Script Language="JavaScript" Src="http://www.anysite/helper/one/help.php?help=adoption&r=1"></Script>";

basically I needed to change these tags on all files in a directory including the sub-directories.

I tried a few examples and got errors.
Your help will be greatly appreciated.
Thanks.
Dave.
 
Old 07-25-2005, 12:45 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Code:
sed 's/SEARCH/REPLACE/g' <infile >outfile
You will need to escape those search and replace fields above, but it should be fairly straightforward.
 
Old 07-25-2005, 06:20 AM   #3
dave4545
Member
 
Registered: Oct 2002
Distribution: Redhat Linux 7.0 Apache/1.3.27 PHP Version 4.3.1 server api Apache
Posts: 46

Original Poster
Rep: Reputation: 15
Thanks.
I'll try it tonight when I get home from work.
 
Old 07-30-2005, 06:14 PM   #4
mhoch3
LQ Newbie
 
Registered: Jul 2005
Posts: 7

Rep: Reputation: 0
Escape characters.... if the sed basic form is

sed s/SEARCHSTRING/REPLACESTRING/g

it is worth knowing that you don't have to use / as the delimiting character. You can use any character you please, pretty much. There are lots and lots of / in the expressions you want to search and replace and if you use / you will have to escape all of them. If instead you use ? which appears only once, or z which I think appears no-where in your urls you will have less escaping to do.

Best Wishes, Max
 
Old 07-31-2005, 12:18 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
And the '|' character is almost never present in strings to be matched.
 
Old 01-27-2006, 10:25 AM   #6
desireuben
LQ Newbie
 
Registered: Jan 2006
Location: New york
Distribution: Mandrake
Posts: 1

Rep: Reputation: 0
hey I also have the same problem. i tried what matir and mhoch3 suggested but the problem is i do not want to create new file with different name, i want to keep the same file name and modify it. if i use same name for infile and outfile, it wipes the file clean.what should i do ?
 
Old 01-27-2006, 11:16 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
perl -pi.bak -e 's/this/that/g' file ... # make a backup

perl -pi -e 's/this/that/g' file ... # don't make a backup
 
Old 01-27-2006, 11:58 AM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Quote:
Originally Posted by desireuben
hey I also have the same problem. i tried what matir and mhoch3 suggested but the problem is i do not want to create new file with different name, i want to keep the same file name and modify it. if i use same name for infile and outfile, it wipes the file clean.what should i do ?
While bigearsbilly's solution may work, this does require the use of perl. Have you considered just making a temporary copy of the file? Like:

Code:
F=`mktemp`
sed '....' < INFILE > $F
mv $F INFILE
rm $F
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using sed to replace string in file umk Debian 12 02-01-2012 09:39 AM
Replace substring with SED marri Programming 2 07-09-2005 06:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 09:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM
Sed - suitable to replace CR LF? J_Szucs Programming 3 05-12-2003 07:03 PM


All times are GMT -5. The time now is 06:33 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration