I currently want to extract a value from a SQL command responde
somehting like this:
psql db -c "SELECT COUNT(test) FROM tbTest;"
the result is:
count
------
33176
(1 row)
I want to extract the 33176 value... is there any simple way to do this?
Why mucking with the unwanted stuff? Simply use some psql
options...
> psql -At -c "SELECT COUNT(test) FROM tbTest;" db
115899
By enclosing the complete shell command in backticks, you can retrieve its result into a shell variable:
#/bin/sh
THECOUNT=`psql -A -t -c 'SELECT COUNT(*) FROM tbTest;' db`
echo "the count = $THECOUNT"
If it is always return in that format (expected result on line 3), you can use this:
psql db -c "SELECT COUNT(test) FROM tbTest;" | tail -n 2 | head -n 1
The explanation:
tail -n 2
will get the last 2 line and then processed byhead -n 1
which mean, get first 1 line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With