[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [feature request] add associative array support to mapfile

From: Léa Gris
Subject: Re: [feature request] add associative array support to mapfile
Date: Thu, 17 Dec 2020 17:58:02 +0100
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Le 17/12/2020 à 17:35, Chet Ramey écrivait :
One problem I see with it is that it assumes the introduction of word
splitting into mapfile, with the burden on the user to ensure that there
are exactly two resultant fields.

It is just meant to be a replacement to looping on:

IFS=<FieldSeparator> read -r -d <record delimiter> k v

So yes it has only two fields per record/line regardless, the first IFS character will delimit the key from the value, and the value is allowed to contain any of IFS except newline, unless the record delimiter is assigned a different character.

The incentive behind this feature request is, to allow mapping key-values pairs from a character stream/file, safely into a Bash's associative array with a single built-in command.

These days, people are going out of their way to parse key value pairs out of foreign config files or result streams from jq processed JSON.

Maybe (it is clearly open to discussion), the associative array mapping could be allowed to:
- skip blank/comment only lines (1)
- allow key without value
- optionally trim trailing comment (1)

Anyway I think some of the above extra parsing would be out of the scope of mapfile, and could be achieved by filtering the stream with sed or other text processor.

Optionally but this would go beyond current Bash shell design IMHO, would be to allow expansion/filter modules to plug into the mapfile command for processing specific file syntax, be it JSON, CSV, configfile, inifile..., something like:
mapfile -m module_name -A assoc_array <input_stream

1. Use a comment delimiter

Léa Gris

reply via email to

[Prev in Thread] Current Thread [Next in Thread]