bash4 read file into associative array

帅比萌擦擦* 提交于 2019-11-27 15:11:10

问题


I am able to read file into a regular array with a single statement:

local -a ary
readarray -t ary < $fileName

Not happening is reading a file into assoc. array.

I have control over file creation and so would like to do as simply as possible w/o loops if possible at all.

So file content can be following to be read in as:

keyname=valueInfo

But I am willing to replace = with another string if cuts down on code, especially in a single line code as above.

And ...

So would it be possible to read such a file into an assoc array using something like an until or from - i.e. read into an assoc array until it hits a word, or would I have to do this as part of loop?

This will allow me to keep a lot of similar values in same file, but read into separate arrays.

I looked at mapfile as well, but does same as readarray.

Finally ...

I am creating an options list - to select from - as below:

local -a arr=("${!1}")
select option in ${arr[*]}; do
    echo ${option}
    break
done

Works fine - however the list shown is not sorted. I would like to have it sorted if possible at all.

Hope it is ok to put all 3 questions into 1 as the questions are similar - all on arrays.

Thank you.


回答1:


First thing, associative arrays are declared with -A not -a:

local -A ary

And if you want to declare a variable on global scope, use declare outside of a function:

declare -A ary

Or use -g if BASH_VERSION >= 4.2.

If your lines do have keyname=valueInfo, with readarray, you can process it like this:

readarray -t lines < "$fileName"

for line in "${lines[@]}"; do
   key=${line%%=*}
   value=${line#*=}
   ary[$key]=$value  ## Or simply ary[${line%%=*}]=${line#*=}
done

Using a while read loop can also be an option:

while IFS= read -r line; do
    ary[${line%%=*}]=${line#*=}
done < "$fileName"

Or

while IFS== read -r key value; do
    ary[$key]=$value
done < "$fileName"


来源:https://stackoverflow.com/questions/25251353/bash4-read-file-into-associative-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!