Re: Better way to process records in bash?

From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: Ron Johnson <ronljohnsonjr(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Better way to process records in bash?
Date: 2024-09-12 19:43:56
Message-ID: CA+v5N41tqJDYxd3JVAz3NKxgPBp28RUnAgQ3h0M1bPNMLmtm+Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Sep 12, 2024 at 6:08 PM Ron Johnson <ronljohnsonjr(at)gmail(dot)com> wrote:

> (This might be a bash question instead of a PG question, or it might be an
> A/B question.)
>
> I need to process table records in a bash script. Currently, I read them
> using a while loop and redirection. The table isn't that big (30ish
> thousand rows), and performance is adequate, but am always looking for
> "better".
>
> Here's the current code:
> declare f1 f3 f8
> while IFS='|' read f1 f3 f8; do
> something f8 f3 f1
> done < <(psql -XAt -c "select f1, f3, f8 from some.table_name;")
>
> --
> Death to America, and butter sauce.
> Iraq lobster!
>

Another approach I've found more enjoyable in some cases,
is using select to_jsonb(...) on the resultset, and then pipe this to jq
<https://jqlang.github.io/jq/>.
This will preserve (more than) decent performance,
something you'll have to give up should you move to python for example.

You'll still have to use bash at some point,
but jq will be more expressive before that happens.
So instead of psql -> bash, you'll have to psql --> jq --> (bash).

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron Johnson 2024-09-12 19:43:59 Re: Better way to process records in bash?
Previous Message Christoph Moench-Tegeder 2024-09-12 19:30:44 Re: Better way to process records in bash?