shell 如何从info.plist文件中生成一个字符串值?

bcs8qyzn  于 2023-05-07  发布在  Shell
关注(0)|答案(2)|浏览(148)

下面是一个示例iOSinfo.plist文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleShortVersionString</key>
    <string>$(MARKETING_VERSION)</string>
    <key>CFBundleVersion</key>
    <string>178273</string>
    <key>NSExtension</key>
</dict>
</plist>

我如何从命令行grep这个文件的字符串值178273?这个178273可以是一个不同的数字。目标是在键CFBundleVersion下grep此值

hsvhsicv

hsvhsicv1#

例如:

cat info.plist | grep -A1 "CFBundleVersion" | grep -o "[0-9]*"

假设

  • 目标必须位于紧跟着关键字CFBundleVersion的行上。
  • 目标必须是数字。其他不能包含数字。
axr492tv

axr492tv2#

plist文件是XML,因此您应该使用能够理解XML的工具,而不是像grep这样的面向文本的工具。在本例中,它可以将XPath表达式应用于文档并打印出匹配的数据。使用常用xmllint实用程序的示例:

$ xmllint --xpath '/plist/dict/key[text()="CFBundleVersion"]/following::string[1]/text()' info.plist
178273

相关问题