从KML文件中删除匹配的嵌套元素标签
我正在尝试使用简单的bash/sed代码从kml文件中删除代码的以下部分。
我熟悉使用sed
删除XML标签,但是,我不确定如何删除与某些名称匹配的子标签。
要重新入学,我需要匹配,然后删除整个“覆盖” < folder></folder>
从kml文件中的标签。
尝试解析所需模式匹配的bash代码:
grep -B 1 "<name>Overlay</name>" -A 9
kml代码:
<Folder>
<name>Overlay</name>
<open>0</open>
<Style>
<ListStyle>
<listItemType>check</listItemType>
<bgColor>00ffffff</bgColor>
<maxSnippetLines>2</maxSnippetLines>
</ListStyle>
</Style>
</Folder>
I am trying to remove the below section of code from a KML file, ideally using simple Bash/sed code.
I am familiar with removing XML tags using sed
but, I'm not sure how to remove sub-tags matching certain names.
To re-iterate, I need to match and then remove the entire "Overlay" <Folder></Folder>
tag from the KML file.
Attempted Bash Code for Parsing Desired Pattern Match:
grep -B 1 "<name>Overlay</name>" -A 9
KML Code:
<Folder>
<name>Overlay</name>
<open>0</open>
<Style>
<ListStyle>
<listItemType>check</listItemType>
<bgColor>00ffffff</bgColor>
<maxSnippetLines>2</maxSnippetLines>
</ListStyle>
</Style>
</Folder>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能对您有用(gnu sed):
收集从
&lt; folder&gt;
到&lt;/folder&gt;
的范围内的行,如果该集合包含&lt ; name&gt; oferlay&lt;/name&gt;
删除它。This might work for you (GNU sed):
Gather up lines in the range from
<Folder>
to</Folder>
and if the collection contains<name>Overlay</name>
delete it.建议一行
gawk
(在大多数Linux计算机中,常规 awk )脚本:Suggesting one line
gawk
(normalawk
in most Linux machines) script:yq 可以用于您的任务。
它是YAML处理器,也可以处理XML。
该语法类似于 jq 。
据我正确理解您,您想删除所有元素,包括它的子数据,
具有子元素
&lt; name&gt; ='覆盖'
。无论嵌套深度如何,以下代码都可以执行此操作。
name =覆盖YQ-Input-Format XML-Output-format XML E'del(.. | select(type ==“ !! MAP”)| select(.name == env(name))) '$ inputfile
输入
输出
yq can be used for your task.
It is a YAML processor and can also deal with XML.
The syntax is similar to jq.
As far as I have understood you correctly, you want to delete all elements including it's child data,
that have as child element
<name> = 'Overlay'
.The following code does this regardless of the nesting depth.
NAME=Overlay yq --input-format xml --output-format xml e 'del(.. | select(type == "!!map") | select(.name == env(NAME)))' $INPUTFILE
Input
Output