日期:2014-05-16  浏览次数:20830 次

求shell 脚本处理如下xml文本

<root>
    <pattern>
        <A></A>
        <B></B>
        <C></C>
    </pattern>
    <pattern>
        <C></C>      <!-- 顺序可能不一样 -->
        <A></A>
        <B></B>
    </pattern>
    <pattern>
        <A></A>
        <B></B>
        <C>1.0</C>   <!-- 只转换那些A,B,C之间全为空的 -->
    </pattern>
</root>


转换后:

<root>
    <pattern>
        <A>0</A>
        <B>0</B>
        <C>0</C>
    </pattern>
    <pattern>
        <C>0</C>      
        <A>0</A>
        <B>0</B>  
    </pattern>
    <pattern>
        <A></A>
        <B></B>
        <C>1.0</C> 
    </pattern>
</root>

------解决方案--------------------
awk -vRS='pattern' '!/>\S+</{gsub(/></,">0<")}{printf $0RS}' test.xml