title: How to replace a text in yml file with sed making sure that the indentation is preserved
body: <p>I have a YAML file with the following content:</p>
  
  <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: castlereport-cfg
    namespace: qa
  data:
    application.properties: |
      BODY
  </code></pre>
  
  <p>I need to replace the BODY with the application.properties file content. I've used sed to do that like this:</p>
  
  <pre><code>sed -i -e '/BODY/r target/classes/application-dev.properties' -e 's///' -e '/^ *\$/d'  target/classes/$kubeConfigFile
  </code></pre>
  
  <p>It does what I need but loses the indentation and I what I get is this:</p>
  
  <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: castlereport-cfg
    namespace: qa
  data:
    application.properties: |
  
  server.port=8080
  server.context-path=/
  
  spring.main.banner-mode=off
  logbook.format.style=http
  </code></pre>
  
  <p>What should I do to keep the indentation? </p>
  
  <p>And one more thing that <code>-e '/^ *\$/d'</code> part in the sed command is there to remove blank lines, but it doesn't seem to work either.</p>
  
  <p>The Expected output is the following:</p>
  
  <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: castlereport-cfg
    namespace: qa
  data:
    application.properties: |
      server.port=8080
      server.context-path=/
      spring.main.banner-mode=off
      logbook.format.style=http   
  </code></pre>
  
  <p>UPDATE:
  I have a Jenkins pipeline script where I am able to achieve what I want with the following three lines:</p>
  
  <pre><code>   sh ( script : "sed -i -e 's/^[ \t\$]*/   /' -e \"/^ *\$/d\"  ./target/classes/"+configFile, returnStdout: true).trim() //remove trailing spaces and blank lines
     sh ( script : "sed -i -e \"/BODY/r ./target/classes/"+configFile+"\" -e \"s///\" ./target/classes/" + configMapKubernetes, returnStdout: true).trim() // insert content of application-dev.properties to YAML file using BODY
     sh ( script : "sed -i -e \"/^ *\$/d\"  ./target/classes/" + configMapKubernetes, returnStdout: true).trim() //remove  blank lines
  </code></pre>
  
  <p>The pipeline script is in groovy so the whole command is inside double-quotes. </p>