Hello

I proposed new psql's format "shell". This format is optimized for
processing returned result in shell:


postgres=# select * from foo;
      a       | b  |     c
--------------+----+------------
 Hello, World | 10 | 2012-05-26
 Ahoj, Svete  | 20 | 2012-06-15
(2 rows)

postgres=# \pset format shell
Output format is shell.
postgres=# select * from foo;
a b c
Hello,\ World 10 2012-05-26
Ahoj,\ Svete 20 2012-06-15

postgres=# \x
Expanded display is on.
postgres=# select * from foo;
( c l )
( [a]=Hello,\ World [b]=10 [c]=2012-05-26 )
( [a]=Ahoj,\ Svete [b]=20 [c]=2012-06-15 )

shell scripts can looks like:

( psql -t -P format=shell postgres <<EOF
SELECT d.datname as "Name",
       pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
       d.datcollate as "Collate",
       d.datctype as "Ctype",
       pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;

EOF
) | while read dbname owner encoding collate ctype priv;
    do
      echo "DBNAME=$dbname OWNER=$owner PRIVILEGES=$priv";
    done;

or:

( psql -t -x -P format=shell postgres <<EOF
SELECT pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
       d.datcollate as "Collate",
       d.datctype as "Ctype",
       pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
   FROM pg_catalog.pg_database d
  ORDER BY 1;
EOF
) | (
while read r
    do
      declare -A row="$r"
      for field in "${!row[@]}"
      do
        echo  "$field -> ${row[$field]}"
      done;
      echo;
    done;)

I invite any comments, mainly from bash or shell experts

Regards

Pavel Stehule

Attachment: format_shell.diff
Description: Binary data

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to