【问题标题】:Use sed or awk to find string and modify text following within xml document使用 sed 或 awk 在 xml 文档中查找字符串并修改以下文本
【发布时间】:2011-12-11 03:08:22
【问题描述】:

我正在尝试在 OS X 上操作 Adob​​e Reader plist 文件以停止自动更新检查。 这需要编写脚本,这样我才能影响 700 多台 Mac 上的更改。

我有:

#!/bin/bash
plutil -convert xml1 /Users/username/Library/Preferences/com.adobe.Reader.plist

转换文件以进行文本编辑。

我在其他地方找到了这个用于多行搜索和替换:

sed -n '1h;1!H;${;g;s/<h2.*</h2>/No title here/g;p;}' sample.php > sample-edited.php;

源文件有重复的类似数据,所以需要在入口CheckForUpdatesAtStartup

之后找到出现的地方

这是文件的一部分:

<key>AutoUriDetect</key>
                    <array>
                            <integer>0</integer>
                            <true/>
                    </array>
                    <key>CheckForUpdatesAtStartup</key>
                    <array>
                            <integer>0</integer>
                            <true/>
                    </array>
                    <key>CheckGPUAtStartup</key>
                    <array>
                            <integer>0</integer>
                            <false/>
                    </array>

所以我需要将 CheckForUpdatesAtStartup 数组的 true 替换为 false

【问题讨论】:

    标签: xml macos bash sed


    【解决方案1】:
    awk > outfile '/CheckForUpdatesAtStartup/, /<(true|false)\/>/ {
      sub(/true/, "false")
      }1' infile
    

    【讨论】:

    • 非常感谢@Dimitre。这是完美的,因为更改是精确的并且不会影响其他键。这是能够在 OS X 世界中操作 plist 的强大脚本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2013-02-18
    • 2011-01-10
    • 2016-03-14
    • 2017-02-18
    • 2019-09-16
    相关资源
    最近更新 更多