Killercode Killercode - 5 months ago 12
Bash Question

Shell Script and SQL results

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?

Answer

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 by head -n 1 which mean, get first 1 line.