slafik
slafik

Reputation: 329

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 ) .

Upvotes: 0

Views: 1065

Answers (1)

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

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

Upvotes: 2

Related Questions