Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sopWl-00E7KR-0w for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2024 19:30:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sopWi-00FZAd-Ks for pgsql-general@arkaria.postgresql.org; Thu, 12 Sep 2024 19:30:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sopWi-00FZ9T-8m for pgsql-general@lists.postgresql.org; Thu, 12 Sep 2024 19:30:48 +0000 Received: from smtp.burggraben.net ([2a01:4f8:140:510a::3]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sopWe-000raU-Il for pgsql-general@postgresql.org; Thu, 12 Sep 2024 19:30:47 +0000 Received: from elch.exwg.net (elch.exwg.net [IPv6:2001:470:7120:1:21b:21ff:fef0:248b]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "elch.exwg.net", Issuer "R10" (verified OK)) by smtp.burggraben.net (Postfix) with ESMTPS id B53C1C0030C; Thu, 12 Sep 2024 21:30:44 +0200 (CEST) Received: by elch.exwg.net (Postfix, from userid 1000) id 69C7CEAC0F; Thu, 12 Sep 2024 21:30:44 +0200 (CEST) Date: Thu, 12 Sep 2024 21:30:44 +0200 From: Christoph Moench-Tegeder To: Ron Johnson Cc: pgsql-general Subject: Re: Better way to process records in bash? Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (2024-03-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ## Ron Johnson (ronljohnsonjr@gmail.com): > 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". Use python, or any other sane language, and don't do bash in python. Anything non-trivial is unreasonably hard to get really right when using shell. Take this example (even when allowing some fuzz with the variables): > 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;") What happens when f3 a) is NULL? b) contains a '|' character? c) contains spaces? (this one depends on how exactly you're handling the shell variables). This might be fine if your data is only NOT NULL numbers, but that's already quite a limitation on your data. Regards, Christoph -- Spare Space