shell - using sed command to replace xml tags -


in project have info.plist (xcode project) file, xml file. add more properties using shell script on terminal.

what need add following:

<key>cfbundleurltypes</key> <array>     <dict>         <key>cfbundletyperole</key>         <string>editor</string>         <key>cfbundleurlname</key>         <string>com.myapp.test</string>         <key>cfbundleurlschemes</key>         <array>             <string>test-scheme</string>         </array>     </dict> </array> 

after doing research found way use sed.

i use sed command replace first occurrence of < dict > tag, in info.plist file, above code (and re-add < dict > @ start of don't break structure.

with of this stackoverflow answer, put following command:

sed ‘0,/<dict>/{s/<dict>/<dict><key>cfbundleurltypes<\/key><array><dict><key>cfbundletyperole<\/key><string>viewer<\/string><key>cfbundleurlname<\/key><string>com.myapp.test<\/string><key>cfbundleurlschemes<\/key><array><string>test-scheme<\/string><\/array><\/dict><\/array>/}’ info.plist 

theoretically, command should replace first occurrence of `< dict >' tag + text want add, without line breaks. skipped /

however when run following error:

-bash: syntax error near unexpected token `<'  

what doing wrong? there should make work? tried skipping < , > without success.

any appreciated.

edit

as suggested in comments jjoao tried replacing sed ‘... ‘ sed '...', following error:

sed: 1: "0,/<dict>/{s/<dict><key ...": unterminated substitute in regular expression 

edit2

for more clarification, desired output:

i have xml file, has following:

<?xml version="1.0" encoding="utf-8"?> <plist version="1.0">     <dict>         <key>cfbundledevelopmentregion</key>         <string>en</string>         .........     </dic> </plist> 

i insert xml section specified @ start. output be:

<?xml version="1.0" encoding="utf-8"?> <plist version="1.0">     <dict><key>cfbundleurltypes</key><array><dict><key>cfbundletyperole</key><string>viewer</string><key>cfbundleurlname</key><string>com.myapp.test</string><key>cfbundleurlschemes</key><array><string>test-scheme</string></array></dict></array>         <key>cfbundledevelopmentregion</key>         <string>en</string>         .........     </dic> </plist> 

or write in cleaner way:

<?xml version="1.0" encoding="utf-8"?> <plist version="1.0">     <dict>         <key>cfbundleurltypes</key>         <array>             <dict>                 <key>cfbundletyperole</key>                 <string>editor</string>                 <key>cfbundleurlname</key>                 <string>com.myapp.test</string>                 <key>cfbundleurlschemes</key>                 <array>                     <string>test-scheme</string>                 </array>             </dict>         </array>         <key>cfbundledevelopmentregion</key>         <string>en</string>         .........     </dic> </plist> 

perl -p behaves sed; perl -p0 reads lines of info.plist file

usage: perl changedict info.plist

#!/usr/bin/perl -p0  $new='         <key>cfbundleurltypes</key>         <array>             <dict>                 <key>cfbundletyperole</key>                 <string>editor</string>                 <key>cfbundleurlname</key>                 <string>com.myapp.test</string>                 <key>cfbundleurlschemes</key>                 <array>                     <string>test-scheme</string>                 </array>             </dict>         </array>';  s!<dict>!<dict>$new!; 

update: previous version writing standard output; can redirect newfile perl changedict info.plist > newinfo.plist.

if need infile substitution, change first line to

#!/usr/bin/perl -pi0 

Comments