Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

bash - bash4 read file into associative array

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...