从info.plist文件中提取字符串值的方法是使用grep命令。

huangapple go评论60阅读模式
英文:

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

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

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 &quot;CFBundleVersion&quot; | grep -o &quot;[0-9]*&quot;

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 &#39;/plist/dict/key[text()=&quot;CFBundleVersion&quot;]/following::string[1]/text()&#39; info.plist
178273

huangapple
  • 本文由 发表于 2023年5月7日 12:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定