LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 10: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-24-2005, 11:45 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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, 05: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, 05: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, 11:18 AM   #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, 09: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, 10:16 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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, 10:58 AM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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



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 08:39 AM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Sed - suitable to replace CR LF? J_Szucs Programming 3 05-12-2003 06:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration