Bash 尝试使用grep从plist中提取数字

Bash 尝试使用grep从plist中提取数字,bash,grep,Bash,Grep,我会尽量把这个问题简短一点。基本上,我正在编写一个shell脚本,我有一个.plist文件,其中包含一个整数值,我正试图“提取”该整数值并将其放入shell脚本中的一个变量中 我能够将.plist文件的内容细化到几行,但仍然得到了一堆不需要的字符 我正在shell脚本中运行以下命令,它将给出以下结果 file_refine=`grep -C 2 CFBundleVersion $file | grep '[0-9]\{3\}'` 输出 <string>645</str

我会尽量把这个问题简短一点。基本上,我正在编写一个shell脚本,我有一个.plist文件,其中包含一个整数值,我正试图“提取”该整数值并将其放入shell脚本中的一个变量中

我能够将.plist文件的内容细化到几行,但仍然得到了一堆不需要的字符

我正在shell脚本中运行以下命令,它将给出以下结果

  file_refine=`grep -C 2 CFBundleVersion $file | grep '[0-9]\{3\}'`
输出

  <string>645</string>
645
我只需要数字,而不是字符串标签,但我似乎无法理解这一点。

试试这个

file_refine=$(grep -C 2 CFBundleVersion $file | grep -o '[0-9]\{3\}')
grep手册页中的-o选项:

 -o, --only-matching
              Print  only  the matched (non-empty) parts of a matching line, with
              each such part on a separate output line.
试试这个

file_refine=$(grep -C 2 CFBundleVersion $file | grep -o '[0-9]\{3\}')
grep手册页中的-o选项:

 -o, --only-matching
              Print  only  the matched (non-empty) parts of a matching line, with
              each such part on a separate output line.

这就成功了,三分钟结束后,如果我不忘记的话,我会给你答案的分数(--)这就成功了,如果我不忘记的话,三分钟结束后,我会给你答案的分数(--)