0

我有一个命令行程序,可以打印出报告,例如:

I found 0 problems on your database,
0 problems were found on your database

等等。

我想编写一个仅将数字导出到变量的 linux shell 脚本。我找不到这样做的正确方法。我怀疑我没有grep正确使用(或者有更好的命令);IE

I found 43 problems on your database,
43 problems were found on your database
$VAR=43

请指教

4

2 回答 2

3
$ var=$(echo -e "I found 43 problems on your database,
43 problems were found on your database" | grep -om 1 '[0-9]\+')
$ echo $var
43
于 2012-06-17T09:56:24.403 回答
1

我发现 Lev Levitsky 的解决方案非常完美,但我只想补充一点,如果您的报告包含许多带有数字的此类行并且您想要处理每一行,您将使用如下内容:

cat report | grep -o '[0-9]\+' | while read var
do
  # do something with var
done
于 2012-06-17T10:43:08.390 回答