Re: psql: print values and return the COUNT(*) value to bash?

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Vincent Veyron <vv(dot)lists(at)wanadoo(dot)fr>
Cc: Ron Johnson <ronljohnsonjr(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: psql: print values and return the COUNT(*) value to bash?
Date: 2025-12-30 05:26:53
Message-ID: aVNinWjzpU3l6yHB@depesz.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Dec 29, 2025 at 05:40:23PM +0100, Vincent Veyron wrote:
> > =$ bash z.sh
> > Rowcount = 1
> > Full output:
> > days_ago | oldest_date | cnt
> > ------------------------+-------------------------------+-----
> > 7 days 03:48:07.348247 | 2025-12-22 07:22:32.593814+01 | 212
> > (1 row)
> >
>
> IIUC, Ron wants the 212 number?
>
> Maybe use the -t switch, and 'cut' :

Ah, that part I misunderstood.

Well, if there is only 1 row returned, always, then the simplest thing
would be to change

> row_count="$( tail -n1 <<< "${returned}" | cut -d "|" -f 3)"

to this:

row_count="$( awk 'NR==3 {print $NF}' <<< "${returned}" )"

format can stay the same, we don't need to psql -t, or -a, and it will
just work :)

Best regards,

depesz

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rahman Duran 2025-12-30 06:30:27 PostgreSQL 18.1 non deterministic collation "LIKE %abc%" performance
Previous Message David G. Johnston 2025-12-30 03:28:09 Combining metavariables and table names