英文:
How to grep a string value from info.plist file?
问题
你可以使用以下命令行命令来从文件中提取 key 为 CFBundleVersion 的字符串值(例如 178273):
grep -o '<key>CFBundleVersion<\/key>[^<]*<string>\([^<]*\)<\/string>' info.plist | sed -n 's/.*<string>\(.*\)<\/string>.*//p'
这将在文件中查找 CFBundleVersion 的值并将其提取出来。如果你的文件名不是 "info.plist",请将命令中的文件名替换为你的实际文件名。
英文:
Here is an example iOS info.plist file
<?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>
How can I grep the string value 178273 from this file from the command line? This 178273 can be a different number. The goal is to grep this value under the key CFBundleVersion
答案1
得分: 1
查找 info.plist 文件中的 "CFBundleVersion",并返回紧跟在其后的数字。
英文:
example:
cat info.plist | grep -A1 "CFBundleVersion" | grep -o "[0-9]*"
Assumption
- The target must be on the line immediately following key CFBundleVersion.
- Target must be a number. Others must not contain numbers.
答案2
得分: 1
Plist文件是XML格式,因此您应该使用了解XML的工具,而不是像grep
这样面向文本的工具。在这种情况下,可以使用能够对文档应用XPath表达式并打印匹配数据的工具。使用常见的xmllint
工具的示例:
$ xmllint --xpath '/plist/dict/key[text()="CFBundleVersion"]/following::string[1]/text()' info.plist
178273
英文:
A plist file is XML, so you should be using tools that understand XML, not text-oriented tools like grep
. In this case, something that can apply an XPath expression to a document and print out the matching data. Example using the common xmllint
utility:
$ xmllint --xpath '/plist/dict/key[text()="CFBundleVersion"]/following::string[1]/text()' info.plist
178273
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论