请注意:您应该提供尽可能多的信息。一般来说,使用正则表达式解析html 、xml、json等不是一个好主意,请始终使用html-parsing - 和 xml-parsing -工具!同时,以下sed代码可能会对您有所帮助。所以还请注意:其他文件和其他结构可能会失败!不要在生产中使用!我假设没有保修!
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>