Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add/remove xml tags using a bash script

Tags:

I have an xml file that I want to configure using a bash script. For example if I had this xml:

<a>    <b>     <bb>         <yyy>             Bla          </yyy>     </bb>   </b>    <c>     <cc>       Something     </cc>   </c>    <d>     bla   </d> </a> 

(confidential info removed)

I would like to write a bash script that will remove section <b> (or comment it) but keep the rest of the xml intact. I am pretty new the the whole scripting thing. I was wondering if anyone could give me a hint as to what I should look into.

I was thinking that sed could be used except sed is a line editor. I think it would be easy to remove the <b> tags however I am unsure if sed would be able to remove all the text between the <b> tags.

I will also need to write a script to add back the deleted section.

like image 500
sixtyfootersdude Avatar asked Apr 01 '10 16:04

sixtyfootersdude


1 Answers

This would not be difficult to do in sed, as sed also works on ranges.

Try this (assuming xml is in a file named foo.xml):

sed -i '/<b>/,/<\/b>/d' foo.xml 

-i will write the change into the original file (use -i.bak to keep a backup copy of the original)

This sed command will perform an action d (delete) on all of the lines specified by the range

# all of the lines between a line that matches <b> # and the next line that matches <\/b>, inclusive /<b>/,/<\/b>/ 

So, in plain English, this command will delete all of the lines between and including the line with <b> and the line with </b>

If you'd rather comment out the lines, try one of these:

# block comment sed -i 's/<b>/<!-- <b>/; s/<\/b>/<\/b> -->/' foo.xml  # comment out every line in the range sed -i '/<b>/,/<\/b>/s/.*/<!-- & -->/' foo.xml 
like image 63
amertune Avatar answered Sep 28 '22 06:09

amertune