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 Code:
cat va_200706.php | sed 's/[downloads/index.php?folder/virginia/200706/]/[dwnld/dl.php?p=]/g' > va_200706_new.php sed s/SEARCHSTRING/REPLACESTRING/g but I think the / in my strings are messing things up. |
Quote:
Eg: Code:
sed 's/downloads\/index.php\?folder\/200706\//dwnld\/new_index\.php\?p\=/' |
And you don't even need to escape the "?" if IIRC. (or the "=" ???).
|
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 |
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 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. |
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 |
oops too many clicks
|
All times are GMT -5. The time now is 12:22 PM. |