簡體   English   中英

使用Regex工具刪除XML標記

[英]Remove XML tags with Regex tools

這是我的XML文件的片段

<layoutItems>
            <behavior>Edit</behavior>
            <field>ID</field>
</layoutItems>
<layoutItems>
            <page>lastViewedAccount</page>
            <showLabel>false</showLabel>
            <showScrollbars>false</showScrollbars>
            <width>100%</width>
</layoutItems>
<layoutItems>
            <behavior>Required</behavior>
            <field>Name</field>
</layoutItems>

我想刪除中間的部分即

<layoutItems>
            <page>lastViewedAccount</page>
            <showLabel>false</showLabel>
            <showScrollbars>false</showScrollbars>
            <width>100%</width>
</layoutItems>

此部分可以與其他標記一起出現在文件內的任何位置。

使用一些字符串操作工具刪除它的最佳方法是什么? 我一直在嘗試與sed運氣但沒有成功。 任何幫助,將不勝感激。

請注意:您應該提供盡可能多的信息。 說一般用解析等等,這不是一個好主意,總是使用 - 和 -tool! 以下代碼可能會幫助您。 所以請注意:它可能與其他文件和其他結構失敗 不要在生產中使用! 我保證保修!

sed -r '/<layoutItems>/{:ka;N;s#(</layoutItems>)#\1#;Tka;s/lastViewedAccount//;T;d}' file 

帶有2個lastViewedAccount標記的lastViewedAccount

    <?xml version="1.0" encoding="UTF-8"?>
    <Layout xmlns="http://test.com/2006/04/metadata">
        <emailDefault>false</emailDefault>
        <headers>PersonalTagging</headers>
        <headers>PublicTagging</headers>
        <layoutSections>
            <customLabel>false</customLabel>
            <detailHeading>false</detailHeading>
            <editHeading>true</editHeading>
            <label>Account Information</label>
            <layoutColumns>
                <layoutItems>
                    <page>lastViewedAccount</page>
                    <showLabel>false</showLabel>
                    <showScrollbars>false</showScrollbars>
                    <width>100%</width>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>OwnerId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Required</behavior>
                    <field>Name</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>ParentId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>AccountNumber</field>
                </layoutItems>
                <layoutItems>
                    <page>lastViewedAccount</page>
                    <showLabel>false</showLabel>
                    <showScrollbars>false</showScrollbars>
                    <width>100%</width>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>Site</field>
                </layoutItems>
            </layoutColumns>
      </layoutSections>
    </Layout>

刪除了lastViewedAccountlastViewedAccount標記:

    <?xml version="1.0" encoding="UTF-8"?>
    <Layout xmlns="http://test.com/2006/04/metadata">
        <emailDefault>false</emailDefault>
        <headers>PersonalTagging</headers>
        <headers>PublicTagging</headers>
        <layoutSections>
            <customLabel>false</customLabel>
            <detailHeading>false</detailHeading>
            <editHeading>true</editHeading>
            <label>Account Information</label>
            <layoutColumns>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>OwnerId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Required</behavior>
                    <field>Name</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>ParentId</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>AccountNumber</field>
                </layoutItems>
                <layoutItems>
                    <behavior>Edit</behavior>
                    <field>Site</field>
                </layoutItems>
            </layoutColumns>
      </layoutSections>
    </Layout>

GNU

sed -nr 'H; \#</layoutItems>#{x;s/(lastViewedAccount)/\1/;Tk;p;:k;x;s/.*//;x;s///;x;d}' file 

$sed -nr 'H; \#</layoutItems>#{x;s/(lastViewedAccount)/\1/;Tk;p;:k;x;s/.*//;x;s///;x;d}' file

    <layoutItems>
            <page>lastViewedAccount</page>
            <showLabel>false</showLabel>
            <showScrollbars>false</showScrollbars>
            <width>100%</width>
    </layoutItems>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM