开发者

how to parse a number from sql output

Hi I need to parse a number from sql output :

  COUNT(*)
----------
       924
       140
       173
       583
       940
        77

6 rows selected.开发者_如何学运维

if the the fisrt line is less then 10 I want to create a empty file, The problem is I dont know how to parse it, the numbers are still changing (from 0 to ca. 10 000 ) .


Question is very unclear so I'll make some assumptions. You'll get the output above from sql either to file or stdout and you would like to test of the first line containing digits is less than 10. Correct?

This is one way to do it.

sed -n '3p' log | awk '{ print ($1 < 10) ? "true" : "false" }'
  • sed is used to print the 3rd line from your example
  • this is then piped into awk which makes to comparison.

...or putting it together in bash

#!/bin/bash

while read variable;
do
    if [[ "$variable" =~ ^[0-9]+$ ]]
        then
        break
    fi
done < input

if [ "$variable" -lt 10 ]
    then
    echo 'less than 10'
    # add your code here, eg
    # touch /path/to/file/to/be/created
fi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜