title: using sed command to replace xml tags
body: <p>in a project I have an info.plist (xcode project) file, which is an xml file. I would like to add more properties to it using a shell script on the terminal.</p>
  
  <p>What I need to add to it is the following:</p>
  
  <pre><code>&lt;key&gt;CFBundleURLTypes&lt;/key&gt;
  &lt;array&gt;
      &lt;dict&gt;
          &lt;key&gt;CFBundleTypeRole&lt;/key&gt;
          &lt;string&gt;Editor&lt;/string&gt;
          &lt;key&gt;CFBundleURLName&lt;/key&gt;
          &lt;string&gt;com.myapp.test&lt;/string&gt;
          &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;
          &lt;array&gt;
              &lt;string&gt;test-scheme&lt;/string&gt;
          &lt;/array&gt;
      &lt;/dict&gt;
  &lt;/array&gt;
  </code></pre>
  
  <p>After doing some research I found that the way to do it is to use the <code>sed</code>.</p>
  
  <p>I will use the <code>sed</code> command to replace the first occurrence of the <code>&lt; dict &gt;</code> tag, which is always in the info.plist file, with the above code (and re-add <code>&lt; dict &gt;</code> at the start of it so I don't break the structure.</p>
  
  <p>With the help of <a href="https://stackoverflow.com/a/9453461/887835">This Stackoverflow answer</a>, I put together the following command:</p>
  
  <pre><code>sed ‘0,/&lt;dict&gt;/{s/&lt;dict&gt;/&lt;dict&gt;&lt;key&gt;CFBundleURLTypes&lt;\/key&gt;&lt;array&gt;&lt;dict&gt;&lt;key&gt;CFBundleTypeRole&lt;\/key&gt;&lt;string&gt;Viewer&lt;\/string&gt;&lt;key&gt;CFBundleURLName&lt;\/key&gt;&lt;string&gt;com.myapp.test&lt;\/string&gt;&lt;key&gt;CFBundleURLSchemes&lt;\/key&gt;&lt;array&gt;&lt;string&gt;test-scheme&lt;\/string&gt;&lt;\/array&gt;&lt;\/dict&gt;&lt;\/array&gt;/}’ info.plist
  </code></pre>
  
  <p>Theoretically, that command should replace the first occurrence of `&lt; dict >' tag with itself + the text I want to add, without line breaks. I also skipped the /</p>
  
  <p>However when I run it I get the following error:</p>
  
  <pre><code>-bash: syntax error near unexpected token `&lt;' 
  </code></pre>
  
  <p>What am I doing wrong? is there something I should do to make this work? I tried skipping all the &lt; and > without success.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As suggested in the comments by JJoao I tried replacing sed ‘... ‘ with sed '...', now I get the following error:</p>
  
  <pre><code>sed: 1: "0,/&lt;dict&gt;/{s/&lt;dict&gt;&lt;key ...": unterminated substitute in regular expression
  </code></pre>
  
  <p><strong>EDIT2</strong></p>
  
  <p>For more clarification, This is my desired output:</p>
  
  <p>I have an xml file, that has the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;plist version="1.0"&gt;
      &lt;dict&gt;
          &lt;key&gt;CFBundleDevelopmentRegion&lt;/key&gt;
          &lt;string&gt;en&lt;/string&gt;
          .........
      &lt;/dic&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <p>I would like to insert the xml section I specified at the start. So the output would be:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;plist version="1.0"&gt;
      &lt;dict&gt;&lt;key&gt;CFBundleURLTypes&lt;/key&gt;&lt;array&gt;&lt;dict&gt;&lt;key&gt;CFBundleTypeRole&lt;/key&gt;&lt;string&gt;Viewer&lt;/string&gt;&lt;key&gt;CFBundleURLName&lt;/key&gt;&lt;string&gt;com.myapp.test&lt;/string&gt;&lt;key&gt;CFBundleURLSchemes&lt;/key&gt;&lt;array&gt;&lt;string&gt;test-scheme&lt;/string&gt;&lt;/array&gt;&lt;/dict&gt;&lt;/array&gt;
          &lt;key&gt;CFBundleDevelopmentRegion&lt;/key&gt;
          &lt;string&gt;en&lt;/string&gt;
          .........
      &lt;/dic&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <p>Or to write it in a cleaner way:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;plist version="1.0"&gt;
      &lt;dict&gt;
          &lt;key&gt;CFBundleURLTypes&lt;/key&gt;
          &lt;array&gt;
              &lt;dict&gt;
                  &lt;key&gt;CFBundleTypeRole&lt;/key&gt;
                  &lt;string&gt;Editor&lt;/string&gt;
                  &lt;key&gt;CFBundleURLName&lt;/key&gt;
                  &lt;string&gt;com.myapp.test&lt;/string&gt;
                  &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;
                  &lt;array&gt;
                      &lt;string&gt;test-scheme&lt;/string&gt;
                  &lt;/array&gt;
              &lt;/dict&gt;
          &lt;/array&gt;
          &lt;key&gt;CFBundleDevelopmentRegion&lt;/key&gt;
          &lt;string&gt;en&lt;/string&gt;
          .........
      &lt;/dic&gt;
  &lt;/plist&gt;
  </code></pre>