's:test1.com:New website is test1.com:g' will not give desired results, as you want to replace test.com with "New website is test1.com" whereas sed will be searcing for "test1.com" which doesn't exist
---------- Post updated at 07:34 AM ---------- Previous update was at 07:31 AM ----------
play with following code, it will make working more clear to you:
echo "<html><body><a href="http://www.test.com">test.com</a></body></html>" | sed -e 's:www.test.com:www.test1.com:g' -e 's:test.com:New website is test1.com:g'
You are correct, it will not give desired results, and your sed suggestion is correct. However, in the code posted by digitalviking, test1.com will exist. The first expression will change www.test.com leaving www.test1.com which the second expression will find and (incorrectly) change.
After the first expression, the record would have been:
EDIT: Did I really miss something? I thought there was a previous post indicating that the output wasn't going to the file. I'll leave this, but out of context (and of course I didn't hit 'quote') it seems I am just rambling. Odd.
Sed writes to standard output. You can use the -i option (if supported by your version of sed) which edits the file in place. (I recommend using the backup feature of the -i option if you go with this. If your sed doesn't support -i, or you want your script to be portable to systems that don't, then you'll need to do something like this:
mv input-file input-file.old
if sed '----your sed programme' input-file.old >input-file
then
rm input-file.old
else
mv input-file.old input-file
fi
Which copies your original file to the old copy and runs the sed on that putting the output into the file with the same name. If the sed is successful, the old file is removed. If the sed fails, the original file is restored.
You probably should also test to ensure that the initial move worked before running the sed.