sed command via SSH not working as expected


I am using openssh from Debian Lenny which is version 1:5.1p1-5.

I am using ssh to run a command on a remote host running Debian Lenny as

The line I am editing with sed looks like this:
'(proxyAddresses=*)' 2>$ERRORLOG | $GREP -if $RELAYDOMAINS | $GREP -Fi
smtp \

I am running the following command to update the line as follows:
ssh root@xxxxxxxxxxxxxxxxxx "sed -i 's/$GREP -Fi smtp/$GREP -Fi smtp |
$SORT -u/' /usr/local/bin/"

But what I find is that the line ends up looking like this on the remote
'(proxyAddresses=*)' 2>$ERRORLOG | $GREP -if $RELAYDOMAINS | $GREP -Fi
smtp | -u \

The important part being "| -u" which should look like "| $SORT -u".

This command works fine when running it on the command line directly.

Could I be missing something or perhaps this is a bug?