How to change specific value of XML attribute using scripting on Mac
-
I have been tasked to write a script to change a specific value in an XML file on about 1000 Macs. Clearly this needs to be scripted, and preferably only using tools that are already available on a Mac (i.e. no additional installs needed). The end goal here is to disable IPv6 in a specific file related to active directory. For example: Old file: <IPv4> <script>Automatic</script> </IPv4> <IPv6> <script>Automatic</script> </IPv6> New file: <IPv4> <script>Automatic</script> </IPv4> <IPv6> <script>__INACTIVE__</script> </IPv6> I have tried searching and have a few sed scripts that get me halfway there, but no where close enough. I can't install any XML parsing programs as this needs to be as automated as possible on all the Macs. Edit: secondary question- using either awk or sed, can I count the number of times that it made a change, i.e. counting the number of instances it found?
-
Answer:
Using awk: awk '/<IPv6>/,/<\/IPv6>/ {sub(/Automatic/,"__INACTIVE__")}1' xml_file > new_xml_file Using sed: In-line editing sed -i '/<IPv6>/,/<\/IPv6>/s/Automatic/__INACTIVE__/' xml_file To add counts in the mix: awk ' /<IPv6>/,/<\/IPv6>/ {sub(/Automatic/,"__INACTIVE__"); if ($0~/__/) count++}1 END{ print FILENAME, count >>"countfile"}' xml_file> new_xml_file The END statement will capture the Filename you ran the script on and the counts of changes in a file called countfile and will keep appending to it for your statistical analysis.
senorsmile at Stack Overflow Visit the source
Related Q & A:
- How to add inline xml schema using jaxb?Best solution by bighow.org
- How to serialize an object to XML with dynamic element's attribute?Best solution by Stack Overflow
- how to fetch a value from persistence.xml to build.properties?Best solution by Stack Overflow
- how elasticsearch exclude a specific value?Best solution by Stack Overflow
- How to change input value in formly?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.