home

Quickies, by Andrea Olivato

text

Format #postgres output for scripting use

How to format a pg query output correctly so that it can be used from a shell script

query="SELECT * FROM sometable";
RESULTS=`psql -d table_name -c "$query" -q -F";" -A`;

for row in $RESULTS ;
do
	# Put your action on each row here
	# you can split it with awk,
	# doing something like
	first=`echo $row|awk'{split($0,a,";");print a[1]}'`;

	# Remember that first row is field description
done;

2 years ago

May 29, 2009
Comments (View)
blog comments powered by Disqus