bash script to edit xml file bash script to edit xml file xml xml

bash script to edit xml file


Using xmlstarlet:

xmlstarlet val -e file.xmlxmlstarlet ed -u "//settings/setting/@name" -v 'local directory2' file.xmlxmlstarlet ed -u "//settings[1]/setting/@name" -v 'local directory2' file.xml# edit file inplacexmlstarlet ed -L -u "//settings/setting/@name" -v 'local directory2' file.xml  


Depending on what you want to do, you may want to use some XML-specific tooling (to handle character encodings, to maintain XML well-formedness etc.). You can use the normal line-oriented tools, but unless you're careful (or doing something trivial) you can easily create non-compliant XML.

I use the XMLStarlet command line set. It's a set of command line utilities for specifically parsing/manipulating XML.


Most people would probably use sed to do line editing from a bash script. If you actually care about parsing the XML, then use something like Perl which has a ready XML parser.