Sujet : Re: Useless Use Of Regexes
De : mh+usenetspam1118 (at) *nospam* zugschl.us (Marc Haber)
Groupes : comp.os.linux.miscDate : 25. Mar 2025, 13:03:45
Autres entêtes
Organisation : private site, see http://www.zugschlus.de/ for details
Message-ID : <vru632$lb7$1@news1.tnib.de>
References : 1 2 3 4
User-Agent : Forte Agent 6.00/32.1186
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.
Greetings
Marc
-- ----------------------------------------------------------------------------Marc Haber | " Questions are the | Mailadresse im HeaderRhein-Neckar, DE | Beginning of Wisdom " | Nordisch by Nature | Lt. Worf, TNG "Rightful Heir" | Fon: *49 6224 1600402