Sujet : Re: Useless Use Of Regexes
De : c186282 (at) *nospam* nnada.net (c186282)
Groupes : comp.os.linux.miscDate : 26. Mar 2025, 05:47:11
Autres entêtes
Message-ID : <kPSdnfFHQI1NG376nZ2dnZfqn_SdnZ2d@giganews.com>
References : 1 2 3 4 5
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0
On 3/25/25 8:03 AM, Marc Haber wrote:
Anssi Saari <anssi.saari@usenet.mail.kapsi.fi> wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>
Don’t forget the option of JSON-format output, for even easier processing
in scripts than messing around with regexes.
>
Kinda yes, but for a script I'd really like to just get the data
directly in an associative array that I can use as is. Instead of
messing around with jq. I just took a look around in Stack Exchange but
meh. One simplish bash example was this:
>
typeset -A myarray
>
while IFS== read -r key value; do
myarray["$key"]="$value"
done < <(jq -r '.SITE_DATA | to_entries | .[] | .key + "=" + .value ' file.json)
>
But that still pukes if the data happens to have a numeric value and in
ip route output at least the metric value is numeric. So apparently it
needs to become .key + "=" + (.value|tostring) to handle that.
This is a classic example why shellscripts are inferior. If you had
written that in python, you could just ingest iproute's output in a
python data structire and access it naturally.
A very wise German person, today a friend of mine, used to say 25
years ago: "Verwende perl. Shell will man können, dann aber nicht
verwenden." In English that would be "Use Perl. You want to be able to
use Shell, but then don't use it." Today, of course, that would be
python.
ANYthing but Perl !!! :-)
Ok, except maybe Lisp ......
It is popular to hate Python these days. Not sure
why except that it's popular/common and, horrors !,
even the NEWBIES can use it.
I do still write bash scripts, IF the job is kinda
simple and straight-forward. However getting 'fancy'
stuff done in scripts soon becomes anything but
transparent ... too many squiggles and punctuation
chars in EXACTLY the right places - a lang extended
and extended and extended kinda ad-hoc until it's
easy to get an unreadable mess. Python is much
easier and more self-documenting. These days it's
even fairly fast. TONS of libraries and examples
out there too.
Today had to make a few mods to a 450-line Python
pgm I wrote about three years ago. Was NOT hard,
could follow what I'd writ before. That's not
always the case with 'C' fer sure :-)
Anyway, generally proto everything in Python these
days, fool around with everything because it's
easy, and then if appropriate port it over to 'C'
or Pascal (still love Pascal !).
Hmmm ... one of the things that attracted me to
the Unix side long long ago was the 'C-shell' ...
yet for some reason I never DID anything with it.
Perhaps time for a second look .....
As for regex ... avoid whenever possible. WORSE
syntax than Bash - gobbeldegoop. More plain to
write a few easy lines of Python for the same
effect ........