title: Using sed to replace a string with the contents of a variable, even if it's an escape character
body: <p>I'm using
  <code>sed -e "s/\*DIVIDER\*/$DIVIDER/g"</code> to replace <code>*DIVIDER*</code> with a user-specified string, which is stored in <code>$DIVIDER</code>. The problem is that I want them to be able to specify escape characters as their divider, like \n or \t. When I try this, I just end up with the letter n or t, or so on.</p>
  
  <p>Does anyone have any ideas on how to do this? It will be greatly appreciated!</p>
  
  <p>EDIT: Here's the meat of the script, I must be missing something.</p>
  
  <pre><code>curl --silent "$URL" &gt; tweets.txt
  
  if [[ `cat tweets.txt` == *\&lt;error\&gt;* ]]; then
      grep -E '(error&gt;)' tweets.txt | \
      sed -e 's/&lt;error&gt;//' -e 's/&lt;\/error&gt;//' |
      sed -e 's/&lt;[^&gt;]*&gt;//g' |
  
  head $headarg | sed G | fmt
  
  else
      echo $REPLACE | awk '{gsub(".", "\\\\&amp;");print}'
      grep -E '(description&gt;)' tweets.txt | \
      sed -n '2,$p' | \
      sed -e 's/&lt;description&gt;//' -e 's/&lt;\/description&gt;//' |
      sed -e 's/&lt;[^&gt;]*&gt;//g' |
      sed -e 's/\&amp;amp\;/\&amp;/g' |
      sed -e 's/\&amp;lt\;/\&lt;/g' |
      sed -e 's/\&amp;gt\;/\&gt;/g' |
      sed -e 's/\&amp;quot\;/\"/g' |
      sed -e 's/\&amp;....\;/\?/g' |
      sed -e 's/\&amp;.....\;/\?/g' |
      sed -e 's/^  *//g' |
      sed -e :a -e '$!N;s/\n/\*DIVIDER\*/;ta' |   # Replace newlines with *divider*.
      sed -e "s/\*DIVIDER\*/${DIVIDER//\\/\\\\}/g" |          # Replace *DIVIDER* with the actual divider.
  
      head $headarg | sed G
  fi
  </code></pre>
  
  <p>The long list of sed lines are replacing characters from an XML source, and the last two are the ones that are supposed to replace the newlines with the specified character. I know it seems redundant to replace a newline with another newline, but it was the easiest way I could come up with to let them pick their own divider. The divider replacement works great with normal characters.</p>