试图用grep从plist中提取一个数字

时间:2012-12-29 01:06:03

标签: bash grep

我会尝试简短地提出这个问题。基本上,我正在开发一个shell脚本,我有一个包含整数值的.plist文件,我试图“提取”并将其放入我的shell脚本中的变量中。

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

我正在我的shell脚本中修改/运行以下命令,它给了我以下结果。

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

输出

  <string>645</string>

我只需要数字数字而不是字符串标签,但我似乎无法弄明白。

1 个答案:

答案 0 :(得分:5)

试试这个

file_refine=$(grep -C 2 CFBundleVersion $file | grep -o '[0-9]\{3\}')

grep man page中的-o选项:

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