[英]Remove XML tags with Regex tools
Here is a snippet of my XML file 这是我的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>
I want to remove the section in the middle ie 我想删除中间的部分即
<layoutItems>
<page>lastViewedAccount</page>
<showLabel>false</showLabel>
<showScrollbars>false</showScrollbars>
<width>100%</width>
</layoutItems>
This section can appear anywhere inside the file along with other tags. 此部分可以与其他标记一起出现在文件内的任何位置。
What is the best way of using some string manipulation tool to remove this? 使用一些字符串操作工具删除它的最佳方法是什么? I have been been trying my luck with
sed
but no success. 我一直在尝试与
sed
运气但没有成功。 Any help would be appreciated. 任何帮助,将不胜感激。
Please note: you should provide as much information as you can. 请注意:您应该提供尽可能多的信息。 Speaking generally parsing of html , xml , json and so on with regex it not a good idea, use always a html-parsing - and xml-parsing -tool!
说一般用正则表达式解析html , xml , json等等,这不是一个好主意,总是使用html-parsing - 和xml-parsing -tool! The following sed code may help you in the mean time.
以下sed代码可能会帮助您。 And so please also note: it may FAIL with other files and other structures!
所以请注意:它可能与其他文件和其他结构失败 ! Do not use in production!
不要在生产中使用! I assume NO warranty!
我保证不保修!
sed -r '/<layoutItems>/{:ka;N;s#(</layoutItems>)#\1#;Tka;s/lastViewedAccount//;T;d}' file
Inputfile with 2 lastViewedAccount
tags: 带有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>
Outputfile, lastViewedAccount
tags removed: 删除了
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>
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.