[英]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
運氣但沒有成功。 任何幫助,將不勝感激。
請注意:您應該提供盡可能多的信息。 說一般用正則表達式解析html , xml , json等等,這不是一個好主意,總是使用html-parsing - 和xml-parsing -tool! 以下sed代碼可能會幫助您。 所以請注意:它可能與其他文件和其他結構失敗 ! 不要在生產中使用! 我保證不保修!
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>
刪除了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>
<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 :
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.