以下是我的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
,但没有成功。任何帮助将不胜感激。
答案 0 :(得分:3)
请注意:您应该提供尽可能多的信息。通常使用html解析xml,json,regex等等,不一个好主意,请始终使用{{3} } - 和html-parsing - 工具!以下xml-parsing代码可能会帮助您。所以请注意:它可能 FAIL 与其他文件和其他结构! 不要在生产中使用!我假设否保修!
sed -r '/<layoutItems>/{:ka;N;s#(</layoutItems>)#\1#;Tka;s/lastViewedAccount//;T;d}' file
包含2个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
个标记:
<?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>
答案 1 :(得分:1)
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>