sed question
hi
i'm tryin to use sed to substitute <td><a href="blah/blah"> with <td><a href="blah/blah2">, and this is for a whole bunch of files so i do: for i in *.html; do sed s/\<td\>\<a\ href=\"blah\/blah\"\>/\<td\>\<a\ href=\"blah\/blah2\"\> "$i"; done and this does not work because of undetermined substitutes. i have no idea where i need the slashes and where i don't. please help! thanks in advance. |
A few things,
1) You need to quote your script for sed sed -e 'your sed script here' 2) Quoting is messy but you just have to keep track of what program is looking at what data. When you use quotes the shell will send the literal string inside. Therefore you don't need to escape inside the quoted sed script except for things that sed needs escaped. Forget about the rules for the shell. Angled brackets don't mean anything special to sed and so they don't need to be escaped. '/' (forward slash) and '"' (double quote mark) need to be escaped 3) Assuming you want to replace multiple instances of that url you need to make your substitution command global. You can do this by appending a g to your substitution command. sed -e 's/you/me/g' 4) You don't want to write over the file you are working on what happens is very implementation defined and varies all over the place. Write your output to a temporary file and mv it to where you want it to be Example: Code:
#/bin/sh will break. It should get you going in the right direction. |
wow, thanks a lot! that really helped!
|
You could always put sed in a script file.
It often makes for an easier to read and reusable tool. Also a lot less worry about quotes and conflicts with the shell! You also can use any delimiter other than '/', e.g '|' Code:
sed is a wonderful tool ;) |
oh cool. i'll try that too, thanks!
|
All times are GMT -5. The time now is 01:23 AM. |