Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 986
mono-dr Avatar asked Aug 11 '14 19:08

mono-dr


1 Answers

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"
like image 167
konsolebox Avatar answered Oct 01 '22 17:10

konsolebox