I need to find a way to use a bash script to take a line like this

" <!-- STANDARD FTP :: DELETE THIS BLOCK FOR SECURE FTP AND UNCOMMENT THE SECURE BLOCK BELOW -->
<tr>
<td valign="middle" align="center" height="35">
<a href="http://!domain!/uftp/">Click here to login to your FTP site.</a>
</td>
</tr>
<!-- END STANDARD FTP -->"

out of an index.html file and replace it with a line like this


"<!-- SECURE FTP :: REMOVE THIS COMMENT/LINE TO ENABLE SECURE FTP
<tr>
<td valign="middle" align="center" height="35">
<a href="https://!domain!:443/!domain!">Click here to login to your Secure FTP site.</a>
</td>
</tr>
// END SECURE FTP :: REMOVE THIS COMMENT/LINE TO ENABLE SECURE FTP -->"


I tried using the sed command and I could not get it to work?


Thanks

sed can be used properly if it is quoted using single quotes, and also escaping(takes away special meaning) some characters with this"\" . lets say I have a text file with these four lines. I saved these to a file called test.txt

abcdefg
hijklm
href="http://!domain!/uftp/">Click FTP site.</a></td></
opqrf

now I want to replace tht url line with this one

href="http://!domain!/uftp/">replacement</a></td></

this way will not work

shane@mainbox shane $ sed "s/href="http://!domain!/uftp/">Click FTP site.</a></td><//href="http://!domain!/uftp/">replacement</a></td></" text.txt
-bash: !domain!/uftp/": event not found

in this example above bash is reading this , !domain!/uftp/", to have special meaning. If we use single quotes with sed, it will remove some specail meaning

sed 's/href="http://!domain!/uftp/">Click FTP site.</a></td><//href="http://!domain!/uftp/">replacement</a></td></' text.txt sed: -e expression #1, char 16: unknown option to `s'

Now the problem is sed uses these characters "/" as markers. Since your string has many of those, "/" in it. Sed is getting all screwed up. You can let sed know that thoee "/" used in your string are not markers. I will give you an example

shane@mainbox shane $ sed 's/href="http:\/\/!domain!\/uftp\/">Click FTP site.<\/a><\/td></\/href="http:\/\/!domain!\/uftp\/">replacement<\/a><\/td><\//' test.txt
abcdefg
hijklm
/href="http://!domain!/uftp/">replacement</a></td><//
opqrf

here is a link, they probably explain escaping better
http://www.tldp.org/LDP/abs/html/quoting.html

So if I put the forward slash and enclose them on backslashes it will work?

you need to do two things
1) use single quotes with sed, not double quotes

sed 's/word/subtitute/'

2)also make sure you escape all instances of "/" like this "\/". Make sure you do not escape any of the three "/" that sed uses to seperate your two strings.

This article has been dead for over six months. Start a new discussion instead.