LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-03-2007, 12:15 AM   #1
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Rep: Reputation: 0
How to replace all occurances of a complex string using sed


Can someone tell me what Im doing wrong. Im trying to replace all occurances of one string with another string.

I want to replace all occurances of downloads/index.php?folder/200706/ with dwnld/new_index.php?p= in a file called va_200706.php

I've tried the following:

Code:
cat va_200706.php | sed 's/\downloads/index.php?folder/virginia/200706/\/\dwnld/dl.php?p=\/g' > va_200706_new.php
as well as

Code:
cat va_200706.php | sed 's/[downloads/index.php?folder/virginia/200706/]/[dwnld/dl.php?p=]/g' > va_200706_new.php
There was a similiar question posted in which the solution was to use

sed s/SEARCHSTRING/REPLACESTRING/g

but I think the / in my strings are messing things up.
 
Old 08-03-2007, 12:23 AM   #2
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 4,448
Blog Entries: 7

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553
Quote:
Originally Posted by 6millionbucks
but I think the / in my strings are messing things up.
You need to 'escape' special characters with backslashes.

Eg:

Code:
sed 's/downloads\/index.php\?folder\/200706\//dwnld\/new_index\.php\?p\=/'
 
Old 08-03-2007, 12:29 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
And you don't even need to escape the "?" if IIRC. (or the "=" ???).
 
Old 08-03-2007, 12:35 AM   #4
6millionbucks
LQ Newbie
 
Registered: Jul 2007
Posts: 10

Original Poster
Rep: Reputation: 0
SOLVED: how to replace all occurances of a complex string

Thanks rkelsen, the backslashes did it.

Heres what I ended up using and it worked wonderfully:

Code:
sed 's\downloads/index.php?folder/virginia/200706\dwnld/dl.php?p=\g' <va_200706.php >va_200706_new.php
 
Old 08-12-2007, 11:45 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
I would have written that as:
Code:
sed 's,downloads/index.php?folder/virginia/200706,dwnld/dl.php?p=,g' <va_200706.php >va_200706_new.php
The principle is the same: the 1st character after the 's' is the regex delimiter. AFAIK, any character may be chosen. I think using '\' is not desirable because then it cannot be used in its normal "escape character" role.

I have come to prefer ',' as my personal standard delimiter, but I have friends who use '!' or '#'. It's a matter of taste.

Although you would not want to use it here because it would require escaping the '?', you might also want to add the sed "-r" to your repertoire -- see the man page.
 
Old 08-12-2007, 11:55 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Also sed can edit the file directly using -i switch and will even make a backup copy.
Code:
sed -i.bak 's\downloads/index.php?folder/virginia/200706\dwnld/dl.php?p=\g' va_200706.php
The above edits the file va_200706.php and creates backup file va_200706.php.bak

Last edited by /bin/bash; 08-12-2007 at 11:59 AM.
 
Old 08-12-2007, 11:55 AM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
oops too many clicks

Last edited by /bin/bash; 08-12-2007 at 11:56 AM. Reason: oops too many clicks
 
  


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
sed replace string octeto Programming 4 06-06-2007 02:09 AM
how many occurances of a substring are they n a string linuxmandrake Programming 1 04-11-2006 02:02 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM

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

All times are GMT -5. The time now is 08:47 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