使用sed命令替换xml标记

使用sed命令替换xml标记,xml,shell,replace,sed,Xml,Shell,Replace,Sed,在一个项目中,我有一个info.plist(xcode项目)文件,它是一个xml文件。我想在终端上使用shell脚本向其添加更多属性 我需要补充以下内容: <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> &

在一个项目中,我有一个info.plist(xcode项目)文件,它是一个xml文件。我想在终端上使用shell脚本向其添加更多属性

我需要补充以下内容:

<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>
<?xml version="1.0" encoding="utf-8"?>
<plist version="1.0">
    <dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        .........
    </dic>
</plist>
理论上,该命令应该将第一次出现的“”标记替换为它本身+我要添加的文本,不带换行符。我也跳过了考试/

但是,当我运行它时,会出现以下错误:

-bash: syntax error near unexpected token `<' 
sed: 1: "0,/<dict>/{s/<dict><key ...": unterminated substitute in regular expression

-bash:意外标记附近的语法错误`
perl-p
的行为类似于sed
perl-p0
读取info.plist文件的所有行

用法:
perl changedict info.plist

#!/usr/bin/perl -p0

my $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!;

为此使用
xsltproc
sed
不是适合这项工作的工具。@TomFenech你甚至可以找到。你会说sed是写电脑游戏的正确工具吗?@TomFenech是的,我知道。只是想让你看看。(希望你还不知道)。玩得开心!:)@hek2mgl即使我没有找到我的问题的答案,我也很高兴我发现sedtris:D太棒了@您是否应该编辑您的问题以向我们展示您想要的输出。谢谢。输出在命令提示符下正常工作,但不会更改info.plist文件。如何使其更改info.plist文件的内容
<?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>
<?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>
#!/usr/bin/perl -p0

my $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!;
#!/usr/bin/perl -pi0