angulardjango angulardjango - 11 days ago 8
Linux Question

Remove XML Nodes using Unix Command Line

I need to remove nodes from a file if they don't have a certain tag.
How can I only keep nodes which have a name tag using awk, sed or grep?

Input:

<node user="user1">
<tag k="name" v="name1"/>
</node>
<node user="user2">
<tag k="network" v="nw1"/>
</node>


Desired output:

<node user="user1">
<tag k="name" v="name1"/>
</node>

Answer

With GNU grep:

grep -Poz '.*<node .*\n.*<tag .*v="name1".*\n.*</node>' file.xml

Output:

   <node user="user1">
      <tag k="name" v="name1"/>
    </node>