pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Formating psql query output
16+ messages / 7 participants
[nested] [flat]

* Formating psql query output
@ 2021-07-19 17:33  Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 3 replies; 16+ messages in thread

From: Rich Shepard @ 2021-07-19 17:33 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Until I finish building the python/tkinter/psycopg2 front end to my business
tracking tool I continue to work using the psql shell.'

I have a working .sql script that reports my contacts between two dates; the
script returns more columns than I want included in the report. I want to
pipe the output through an awk script to extract, in order, the columns I
need. I'm stuck at the point of defining options to psql.

The current command line is:
psql -d bustrac -f prospecting_log.sql -o contacts.txt --csv

However, using the --csv output conversion makes separate fields from a
varchar column that can contain commas with the text contents.

Without --csv I get normal psql output with column headings and separator
lines such as these:
  person_nbr |  act_date  | act_type  |                                                                                                                                                                                      notes                                                                                                                                                                                      | person_nbr |      lname       |   fname    | org_nbr | org_nbr |       org_name 
------------+------------+-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------+------------------+------------+---------+---------+----------------------

Is there an option that will retain the '|' separator but exclude the
headings? Reading the psql document page I don't see such an option.

TIA,

Rich







^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* RE: Formating psql query output
@ 2021-07-19 17:41  Basques, Bob (CI-StPaul) <bob.basques@ci.stpaul.mn.us>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  2 siblings, 1 reply; 16+ messages in thread

From: Basques, Bob (CI-StPaul) @ 2021-07-19 17:41 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 17:42  David Santamauro <david.santamauro@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  2 siblings, 0 replies; 16+ messages in thread

From: David Santamauro @ 2021-07-19 17:42 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>; +Cc: GMAIL <david.santamauro@gmail.com>


From: Rich Shepard <rshepard@appl-ecosys.com>
Date: Monday, July 19, 2021 at 1:33 PM

> Is there an option that will retain the '|' separator but exclude the headings?
> Reading the psql document page I don't see such an option.


echo 'select 1,2,3,4;' | psql -At -F'|'

1|2|3|4


-A Switches to unaligned output mode. (The default output mode is aligned.) This is equivalent to \pset format unaligned.
-t Turn off printing of column names and result row count footers, etc. This is equivalent to \t or \pset tuples_only.
-F Use separator as the field separator for unaligned output. This is equivalent to \pset fieldsep or \f.

HTH

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 17:49  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  2 siblings, 2 replies; 16+ messages in thread

From: Adrian Klaver @ 2021-07-19 17:49 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org

On 7/19/21 10:33 AM, Rich Shepard wrote:
> Until I finish building the python/tkinter/psycopg2 front end to my 
> business
> tracking tool I continue to work using the psql shell.'
> 
> I have a working .sql script that reports my contacts between two dates; 
> the
> script returns more columns than I want included in the report. I want to
> pipe the output through an awk script to extract, in order, the columns I
> need. I'm stuck at the point of defining options to psql.

Is there a reason you can't just restrict the query to the columns you want?


> 
> TIA,
> 
> Rich
> 
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 18:01  Rob Sargent <robjsargent@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 0 replies; 16+ messages in thread

From: Rob Sargent @ 2021-07-19 18:01 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org



> On Jul 19, 2021, at 11:49 AM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> 
> On 7/19/21 10:33 AM, Rich Shepard wrote:
>> Until I finish building the python/tkinter/psycopg2 front end to my business
>> tracking tool I continue to work using the psql shell.'
>> I have a working .sql script that reports my contacts between two dates; the
>> script returns more columns than I want included in the report. I want to
>> pipe the output through an awk script to extract, in order, the columns I
>> need. I'm stuck at the point of defining options to psql.
> 
> Is there a reason you can't just restrict the query to the columns you want?
> 

Also had that thought but OP is wise to head the other switches for cleaner output.  But also should probably get familiar with to-json options as well for easy pickup on the eventual client.






^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* RE: Formating psql query output
@ 2021-07-19 18:53  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Basques, Bob (CI-StPaul) <bob.basques@ci.stpaul.mn.us>
  0 siblings, 1 reply; 16+ messages in thread

From: Rich Shepard @ 2021-07-19 18:53 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Mon, 19 Jul 2021, Basques, Bob (CI-StPaul) wrote:

> -F separator
> --field-separator=separator
> Use separator as the field separator for unaligned output. This is equivalent to \pset fieldsep or \f.

Bobb,

I should have mentioned that I tried that. Without the --csv option the results
have the headings and separator. With --csv the field separator is ignored
regardless of postion within the command string.

Thanks,

Rich





^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 18:58  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 1 reply; 16+ messages in thread

From: Rich Shepard @ 2021-07-19 18:58 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Mon, 19 Jul 2021, Adrian Klaver wrote:

> Is there a reason you can't just restrict the query to the columns you want?

Adrian,

As far as I know I need to specify FK and PK columns when tables are joined;
I don't need those key columns in the output.

Thanks,

Rich





^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 18:59  Rob Sargent <robjsargent@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Rob Sargent @ 2021-07-19 18:59 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>



> On Jul 19, 2021, at 12:53 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
> 
> On Mon, 19 Jul 2021, Basques, Bob (CI-StPaul) wrote:
> 
>> -F separator
>> --field-separator=separator
>> Use separator as the field separator for unaligned output. This is equivalent to \pset fieldsep or \f.
> 
> Bobb,
> 
> I should have mentioned that I tried that. Without the --csv option the results
> have the headings and separator. With --csv the field separator is ignored
> regardless of postion within the command string.
Can we see on line of the csv output?  The field with commas should be in quotes, no?  You’ll have write a “real” csv importer. awk =F”\”*,*\”” might, heavy on the might.






^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 19:07  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Rob Sargent <robjsargent@gmail.com>
  0 siblings, 2 replies; 16+ messages in thread

From: Rich Shepard @ 2021-07-19 19:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Mon, 19 Jul 2021, Rob Sargent wrote:

> Can we see on line of the csv output? The field with commas should be in
> quotes, no? You’ll have write a “real” csv importer. awk =F”\”*,*\””
> might, heavy on the might.

Rob,

Here's a redacted output line:

8,2019-04-08,Phone,Went to voice mail @ 14:48; didn't leave a message. Call
Wednesday morning,8,<lname>,<fname>,537,537,<company_name>

No quoted text fields.

Rich






^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 19:11  Rob Sargent <robjsargent@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 1 reply; 16+ messages in thread

From: Rob Sargent @ 2021-07-19 19:11 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>




> On Jul 19, 2021, at 1:07 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote:
> 
> On Mon, 19 Jul 2021, Rob Sargent wrote:
> 
>> Can we see on line of the csv output? The field with commas should be in
>> quotes, no? You’ll have write a “real” csv importer. awk =F”\”*,*\””
>> might, heavy on the might.
> 
> Rob,
> 
> Here's a redacted output line:
> 
> 8,2019-04-08,Phone,Went to voice mail @ 14:48; didn't leave a message. Call
> Wednesday morning,8,<lname>,<fname>,537,537,<company_name>
> 
> No quoted text fields.
> 
> Rich
> 
> 
Postgres version?
> 






^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 19:11  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Adrian Klaver @ 2021-07-19 19:11 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org

On 7/19/21 11:58 AM, Rich Shepard wrote:
> On Mon, 19 Jul 2021, Adrian Klaver wrote:
> 
>> Is there a reason you can't just restrict the query to the columns you 
>> want?
> 
> Adrian,
> 
> As far as I know I need to specify FK and PK columns when tables are 
> joined;

You need them in the JOIN and/or WHERE sections, but not necessarily in 
the field list in the SELECT portion.

> I don't need those key columns in the output.
> 
> Thanks,
> 
> Rich
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 19:12  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 1 reply; 16+ messages in thread

From: Pavel Stehule @ 2021-07-19 19:12 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

po 19. 7. 2021 v 21:07 odesílatel Rich Shepard <rshepard@appl-ecosys.com>
napsal:

> On Mon, 19 Jul 2021, Rob Sargent wrote:
>
> > Can we see on line of the csv output? The field with commas should be in
> > quotes, no? You’ll have write a “real” csv importer. awk =F”\”*,*\””
> > might, heavy on the might.
>
> Rob,
>
> Here's a redacted output line:
>
> 8,2019-04-08,Phone,Went to voice mail @ 14:48; didn't leave a message. Call
> Wednesday morning,8,<lname>,<fname>,537,537,<company_name>
>
> No quoted text fields.
>

text fields are quoted only when it is necessary

[pavel@localhost src]$ psql -c "select 'ahoj' as x, 'svete' as y" --csv
Assertions: on
x,y
ahoj,svete
[pavel@localhost src]$ psql -c "select 'ahoj' as x, 'sve,te' as y" --csv
Assertions: on
x,y
ahoj,"sve,te"

If you need forced quoting, you need to use COPY TO STDOUT statement

Regards

Pavel

>
> Rich
>
>
>
>

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 19:21  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 0 replies; 16+ messages in thread

From: Pavel Stehule @ 2021-07-19 19:21 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

po 19. 7. 2021 v 21:12 odesílatel Pavel Stehule <pavel.stehule@gmail.com>
napsal:

>
>
> po 19. 7. 2021 v 21:07 odesílatel Rich Shepard <rshepard@appl-ecosys.com>
> napsal:
>
>> On Mon, 19 Jul 2021, Rob Sargent wrote:
>>
>> > Can we see on line of the csv output? The field with commas should be in
>> > quotes, no? You’ll have write a “real” csv importer. awk =F”\”*,*\””
>> > might, heavy on the might.
>>
>> Rob,
>>
>> Here's a redacted output line:
>>
>> 8,2019-04-08,Phone,Went to voice mail @ 14:48; didn't leave a message.
>> Call
>> Wednesday morning,8,<lname>,<fname>,537,537,<company_name>
>>
>> No quoted text fields.
>>
>
> text fields are quoted only when it is necessary
>
> [pavel@localhost src]$ psql -c "select 'ahoj' as x, 'svete' as y" --csv
> Assertions: on
> x,y
> ahoj,svete
> [pavel@localhost src]$ psql -c "select 'ahoj' as x, 'sve,te' as y" --csv
> Assertions: on
> x,y
> ahoj,"sve,te"
>
> If you need forced quoting, you need to use COPY TO STDOUT statement
>

[pavel@localhost src]$ psql -c "copy (select 'ahoj' as x, 'svete' as y) to
stdout force quote * csv header"
Assertions: on
x,y
"ahoj","svete"


>
> Regards
>
> Pavel
>
>>
>> Rich
>>
>>
>>
>>

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 20:04  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Rob Sargent <robjsargent@gmail.com>
  0 siblings, 0 replies; 16+ messages in thread

From: Rich Shepard @ 2021-07-19 20:04 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Mon, 19 Jul 2021, Rob Sargent wrote:

> Postgres version?

postgresql-12.7-x86_64-1_SBo





^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 20:05  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Rich Shepard @ 2021-07-19 20:05 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Mon, 19 Jul 2021, Adrian Klaver wrote:

> You need them in the JOIN and/or WHERE sections, but not necessarily in the 
> field list in the SELECT portion.

Adrian,

I wondered about that and thought I needed to include them in the SELECT
phrase.

Thanks for the lesson.

Regards,

Rich





^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Formating psql query output
@ 2021-07-19 20:44  Ron <ronljohnsonjr@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 0 replies; 16+ messages in thread

From: Ron @ 2021-07-19 20:44 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 7/19/21 3:05 PM, Rich Shepard wrote:
> On Mon, 19 Jul 2021, Adrian Klaver wrote:
>
>> You need them in the JOIN and/or WHERE sections, but not necessarily in 
>> the field list in the SELECT portion.
>
> Adrian,
>
> I wondered about that and thought I needed to include them in the SELECT
> phrase.

If that were the case, then there would be no purpose in enumerating columns 
instead of writing "SELECT T1.*, T2.*"

-- 
Angular momentum makes the world go 'round.





^ permalink  raw  reply  [nested|flat] 16+ messages in thread


end of thread, other threads:[~2021-07-19 20:44 UTC | newest]

Thread overview: 16+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-07-19 17:33 Formating psql query output Rich Shepard <rshepard@appl-ecosys.com>
2021-07-19 17:41 ` Basques, Bob (CI-StPaul) <bob.basques@ci.stpaul.mn.us>
2021-07-19 18:53   ` Rich Shepard <rshepard@appl-ecosys.com>
2021-07-19 18:59     ` Rob Sargent <robjsargent@gmail.com>
2021-07-19 19:07       ` Rich Shepard <rshepard@appl-ecosys.com>
2021-07-19 19:11         ` Rob Sargent <robjsargent@gmail.com>
2021-07-19 20:04           ` Rich Shepard <rshepard@appl-ecosys.com>
2021-07-19 19:12         ` Pavel Stehule <pavel.stehule@gmail.com>
2021-07-19 19:21           ` Pavel Stehule <pavel.stehule@gmail.com>
2021-07-19 17:42 ` David Santamauro <david.santamauro@gmail.com>
2021-07-19 17:49 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-07-19 18:01   ` Rob Sargent <robjsargent@gmail.com>
2021-07-19 18:58   ` Rich Shepard <rshepard@appl-ecosys.com>
2021-07-19 19:11     ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-07-19 20:05       ` Rich Shepard <rshepard@appl-ecosys.com>
2021-07-19 20:44         ` Ron <ronljohnsonjr@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox