如何在Mac上使用脚本更改XML属性的特定值 [英] How to change specific value of XML attribute using scripting on Mac

查看:526
本文介绍了如何在Mac上使用脚本更改XML属性的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我受命编写脚本来更改大约1000台Mac上XML文件中的特定值.显然,这需要编写脚本,并且最好仅使用Mac上已经可用的工具(即无需额外安装).此处的最终目标是在与活动目录相关的特定文件中禁用IPv6.例如:

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:

<IPv4>
    <script>Automatic</script>
</IPv4>

<IPv6>
    <script>Automatic</script>
</IPv6>

新文件:

<IPv4>
    <script>Automatic</script>
</IPv4>

<IPv6>
    <script>__INACTIVE__</script>
</IPv6>

我尝试搜索,并且有一些sed脚本可以将我带到一半,但是距离不够近.我无法安装任何XML解析程序,因为这需要在所有Mac上尽可能自动化.

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.

次要问题-

我可以使用awk或sed来计算更改的次数,即计算发现的实例的次数吗?

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?

推荐答案

使用awk:

Using awk:

awk '/<IPv6>/,/<\/IPv6>/ {sub(/Automatic/,"__INACTIVE__")}1' xml_file > new_xml_file

使用sed:内联编辑

Using sed: In-line editing

sed -i '/<IPv6>/,/<\/IPv6>/s/Automatic/__INACTIVE__/' xml_file

要在组合中添加计数:

awk '
/<IPv6>/,/<\/IPv6>/ {sub(/Automatic/,"__INACTIVE__"); if ($0~/__/) count++}1 
END{ print FILENAME, count >>"countfile"}' xml_file> new_xml_file

END语句将捕获运行脚本的Filename和更改counts在名为countfile的文件中,并将appending保留在其中以进行统计分析.

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.

这篇关于如何在Mac上使用脚本更改XML属性的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆