SSN
SSN

Reputation: 886

Remove "set of xml tag" using sed

I have huge xml file. i want to match particular string and delete whole related tag example xml

<server>
    <servlet-name>default</servlet-name>
    <servlet-class>servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</server>

In this xml i want to match 3 things <server>..<servlet-name>default</servlet-name>....</server>. if it matches 3 thing delete whole thing. I tried sed command below.

sed -i '/server/,/server/ d' web1.xml

So in this thing sever to sever whole thing will be delete. but i want match 3rd thing also default like below command

sed -i '/server/,/webnav/,/server/ d' web1.xml

this command not working but i want to like this

Upvotes: 1

Views: 1987

Answers (1)

SLePort
SLePort

Reputation: 15461

As mentionned in your comments, sed is not the right tool for parsing xml.

That being said, if you have no choice you can try this one (at your own risk):

sed '/<server>/{:a;/<\/server>/!{N;ba;}};/<servlet-name>default<\/servlet-name>/d;' web1.xml

Lines between <server> and </server> are merged and then deleted if the string <servlet-name>default</servlet-name> is found.

Explanation:

  • /<server>/: if <server> is found
  • :a: label a for loop to come
  • /<\/server>/!: if </server> is not found:
  • N: appends next line to pattern space
  • ba: loops to :a label
  • /<servlet-name>default<\/servlet-name>/: when loop ends, search for string <servlet-name>default</servlet-name> in pattern space. And if found:
  • d: clean the pattern space (ie deletes all from <server> to </server>

Upvotes: 2

Related Questions