title: Replacing sed with sed on RHEL6.7
body: <p>I am trying to replace a sed command with a sed command and it keeps falling over so after a few hours of "picket fencing" I thought I would ask the question here.</p>
  
  <p>I have various bash scripts that contain this kind of line:</p>
  
  <pre><code> sed 's/a hard coded server name servername.//'
  </code></pre>
  
  <p>I would like to replace it with:</p>
  
  <pre><code> sed "s/a hard coded server name $(hostname).//"
  </code></pre>
  
  <p>Note the addition of double quotes so that the $(hostname) is expanded which make this a little trickier than I expected.</p>
  
  <p>So this was my first of many failed attempts:</p>
  
  <pre><code>cat file | sed 's!sed \'s\/a hard coded server name servername.\/\/\'!sed \"s\/a hard coded server name $(hostname).\/\/\"!g'
  </code></pre>
  
  <p>I also tried using sed's nice "-e" option to break down the replace into parts to try and target the problem areas. I wouldn't use the "-e" switch in a solution but it is useful sometimes for debugging:</p>
  
  <pre><code>cat file | sed -e 's!servername!\$\(hostname\)!' -e 's!\| sed \'s!\| sed \"s!'
  </code></pre>
  
  <p>The first sed works as expected (nothing fancy happening here) and the second fails so no point adding the third that would have to replace the closing double quote.</p>
  
  <p>At this point my history descends into chaos so no point adding any more failed attempts. </p>
  
  <p>I wanted to use the first replacement in a single command as the script is full of sed commands and I wanted to target just one specific command in the script.</p>
  
  <p>Any ideas would be appreciated. </p>