title: Shell execute grep command within sed replacement not as expected
body: <p>I am working on some deployment script for Kubernetes, and want to execute a single command to replace some &quot;Template Variables&quot; in yaml file using sed.</p>
  <p>I have the following (example, shortened yaml file) input file where i want to do the replacements:<br />
  input.txt</p>
  <pre><code>CONTAINER_ADDITIONAL
  spec:
    selector:
      app: CONTAINER_NAME
    type: NodePort
    ports:
      - protocol: TCP
        port: EXPOSED_PORT
  </code></pre>
  <p>Also, I've got the following Dockerfile where i want to get the <code>EXPOSE</code> port number from to be inserted into the <code>EXPOSED_PORT</code>:<br />
  Dockerfile</p>
  <pre><code>FROM node:latest
  
  EXPOSE 3000
  WORKDIR /app
  </code></pre>
  <p>I have now tried to use different approaches to get the port number <code>3000</code> (from Dockerfile) inserted into the <code>EXPOSED_PORT</code><br />
  (<code>CONTAINER_NAME</code> and <code>CONTAINER_ADDITIONAL</code> are working, they are in the example file for completenes with the command below).</p>
  <p>The following commands can be executed directly in the shell and give the wanted result (<code>3000</code>):<br />
  <code>cat Dockerfile | grep EXPOSE | cut -d&quot; &quot; -f2</code> -&gt; there may be still the <code>\n</code><br />
  <code>cat Dockerfile | grep EXPOSE | cut -d&quot; &quot; -f2 | tr -d &quot;\n&quot;</code> -&gt; previously mentionned <code>\n</code> removed<br />
  <code>grep EXPOSE Dockerfile | cut -d&quot; &quot; -f2</code> -&gt; with <code>\n</code><br />
  <code>grep EXPOSE Dockerfile | cut -d&quot; &quot; -f2 | tr -d &quot;\n&quot;</code> -&gt; without <code>\n</code><br />
  <code>grep EXPOSE Dockerfile | awk '{print $2}'</code> -&gt; with <code>\n</code>, uses single quote - not an option?<br />
  <code>grep EXPOSE Dockerfile|tr -d -c 0-9</code> -&gt; without <code>\n</code>, not prefered (when situation would exist of multiple port numbers separated by spaces)<br />
  <code>grep -Po &quot;(?&lt;=^EXPOSE )\w*$&quot; Dockerfile</code> -&gt; with <code>\n</code>, not prefered (multiple port numbers)
  <code>grep -Po &quot;(?&lt;=^EXPOSE )\w*$&quot; Dockerfile | tr -d &quot;\n&quot;</code> -&gt; without <code>\n</code></p>
  <p><strong>HOWEVER:</strong></p>
  <p><code>sed -e 's@CONTAINER_NAME@some-container-name@g;s@CONTAINER_ADDITIONAL@cat some_config.txt@e;s@EXPOSED_PORT@grep EXPOSE Dockerfile | cut -d&quot; &quot; -f2 | tr -d &quot;\n&quot;@e' input.txt</code></p>
  <p>Does not work for the <code>EXPOSED_PORT</code>. The other two variables <code>CONTAINER_NAME</code> and <code>CONTAINER_ADDITIONAL</code> work (the cat gets executed, content of <code>some_config.txt</code> is being put in there)</p>
  <p>No matter which of the above mentioned commands that are working and giving the correct result directly in shell, they do not work when executed in sed (the awk for sure not, because of single quotes).</p>
  <p>The output I get is:</p>
  <pre><code>inserted_content_of: some_config.txt
  some_more_inserted_content_from: some_config.txt
  spec:
    selector:
      app: some-container-name
    type: NodePort
    ports:
      - protocol: TCP
  sh: 1: port:: not found
  </code></pre>
  <p>The expected output that i want to have:</p>
  <pre><code>inserted_content_of: some_config.txt
  some_more_inserted_content_from: some_config.txt
  spec:
    selector:
      app: some-container-name
    type: NodePort
    ports:
      - protocol: TCP
        port: 3000
  </code></pre>
  <p>Is there anything that I am doing wrong with the sed command?<br />
  Is there some explanation what goes wrong?<br />
  How can i solve this issue?</p>