MxfrdA MxfrdA - 1 year ago 145
Bash Question

Edit xml file shell script

I want to edit a xml file using a shell script.

i've got something like this


And i want my script to edit the .xml file like this, using a variable

ex : $value = 3.2
'' command to change the xml file '' and get this :


I've been looking on the web but nothing works for me ..

Edit: I'm looking for a generic way.

For example the solution of this post : How to change specific value of XML attribute using scripting on Mac

isn't what i'm looking for, because it depends of the previous xml file.


With sed :

$ ver=3.2;
$ sed "s/\(<version>\)[^<]*\(<\/version>\)/\1$ver\2/" <<< "<version>1.7.21</version>"

To apply it to file :

$ ver=3.2;
$ sed -i "s/\(<version>\)[^<]*\(<\/version>\)/\1$ver\2/" file

The -i option is for editing the file in place.

Update :

To apply a sed command to a path, you must either escape the slashes in the path :


or change the sed separator with a character you won't find in your path. Example with | :

sed "s|\(<version>\)[^<]*\(<\/version>\)|\1$ver\2|"