Search code examples
bashsedmime

bash replace mime placeholder with multiple line


I have a template email in MIME and part of the email body is the placeholder ${result}.

  <body>
      <p>${result}</p>
  </body>

In the bash script, I have a variable $RESULT that store the following table in a string.

----------------------------------------------------
           Sydney    Tokyo      London     New York      
product1      100      120           0           70
product2        4       80         210          110
product3      150       50          80           20
----------------------------------------------------

I've got couple of more placeholder need to be replaced(just a short string or a word), so I have used the following sed command, however it returns an error:

sed -e "s/\${subject}/$1/" \
    -e "s/\${client}/$NAME/" \
    -e "s/\${result}/$RESULT/" temp-email > client-email

Error:

sed: -e expression #3, char 64: unterminated `s' command

I know it is something to do with the \n, but I am just start learning bash and MIME, not sure how I can replace the placeholder with $RESULT

Thanks in advance


Solution

  • You can replace newlines by \n's usin parameter expansion

    RESULT=${RESULT//$'\n'/'\n'}
    

    Or use Perl than can handle newlines in replacements:

    CLIENT=$NAME RESULT=$RESULT SUBJECT=$1 perl -pe '
        s/\${(subject|client|result)}/$ENV{ uc $1 }/g'