Home > database >  Remove Matched Nested Element Tag from KML File
Remove Matched Nested Element Tag from KML File

Time:05-13

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>

CodePudding user response:

This might work for you (GNU sed):

sed '/<Folder>/{:a;N;/<\/Folder>/!ba;/<name>Overlay<\/name>/d}' file

Gather up lines in the range from <Folder> to </Folder> and if the collection contains <name>Overlay</name> delete it.

CodePudding user response:

Suggesting one line gawk (normal awk in most Linux machines) script:

  gawk '/<Folder>/,/<\/Folder>/{next}1' input.kml
  • Related