Take the 2-minute tour ×

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:



New file:



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?

share|improve this question
Did my updated answer resolve your second question? –  jaypal Dec 15 '11 at 8:54
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
Wow! That was a quick response and works like a charm! Thanks. –  senorsmile Dec 15 '11 at 4:15
You're welcome. I added an alternate sed solution. If the solution works for you feel free to up vote and accept the answer. :) Good luck!! –  jaypal Dec 15 '11 at 4:18
I am new to SO, but have been on SF a while. I had forgotten it makes you wait to accept answers. –  senorsmile Dec 15 '11 at 4:20
Thanks for adding the example using sed. I swear I had tried something almost exactly like that and it didn't work. On second thought, I think I had the single quotes in the wrong place. –  senorsmile Dec 15 '11 at 4:23
Yep, missing or incorrect location of quotes can lead to several hours of frustration. :) –  jaypal Dec 15 '11 at 4:26
show 2 more comments

Your Answer


