shell解析xml文件

centos安装xml2

yum install xml2

测试文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

    <properties>
        <comment>
            PowerEngine Main Configuration
        </comment>
        <entry key="CurrentMode">Mode.DEV</entry>

        <entry key="Mode.DEV">
            classpath:/config/mode/dev/peconfig.xml
        </entry>

        <entry key="Mode.TEST">
            classpath:/config/mode/test/peconfig.xml
        </entry>

        <entry key="Mode.PRO">
            classpath:/config/mode/pro/peconfig.xml
        </entry>

        <entry key="Mode.UAT">
            classpath:/config/mode/uat/peconfig.xml
        </entry>
        <entry key="CommonConfig">
            classpath:/config/pe/*.xml
            classpath:/config/dubbo/*.xml,
            classpath:/config/trs/*.xml,
            classpath:/config/db/*.xml
        </entry>

解析文件并替换内容

xml2 < test.xml > test.txt
sed -i 's#/properties/entry=Mode.DEV#/properties/entry=Mode.PRO#g' test.txt
/properties/comment=
/properties/comment=        PowerEngine Main Configuration
/properties/comment=
/properties/entry/@key=CurrentMode
/properties/entry=Mode.DEV
/properties/entry
/properties/entry/@key=Mode.DEV
/properties/entry=
/properties/entry=		classpath:/config/mode/dev/peconfig.xml
/properties/entry=
/properties/entry
/properties/entry/@key=Mode.TEST
/properties/entry=
/properties/entry=		classpath:/config/mode/test/peconfig.xml
/properties/entry=
/properties/entry
/properties/entry/@key=Mode.PRO
/properties/entry=
/properties/entry=		classpath:/config/mode/pro/peconfig.xml
/properties/entry=
/properties/entry
/properties/entry/@key=Mode.UAT
/properties/entry=
/properties/entry=		classpath:/config/mode/uat/peconfig.xml
/properties/entry=
/properties/entry
/properties/entry/@key=CommonConfig
/properties/entry=
/properties/entry=		classpath:/config/pe/*.xml
/properties/entry=		classpath:/config/dubbo/*.xml,
/properties/entry=		classpath:/config/trs/*.xml,
/properties/entry=		classpath:/config/db/*.xml
/properties/entry=

恢复为xml格式文件

echo '<?xml version="1.0" encoding="UTF-8" ?>' > test.xml
echo '<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">' >> test.xml
2xml < test.txt >> test.xml
posted @ 2019-08-06 10:08  培天王  阅读(2610)  评论(0编辑  收藏  举报