I'm trying to read a structured file into an associative array in Bash. The file stores in each line a person name and a person address. For example:
person1|address1
person2|address2
...
personN|addressN
I am using the script below.
#!/bin/bash
declare -A address
while read line
do
name=`echo $line | cut -d '|' -f 1`
add=`echo $line | cut -d '|' -f 2`
address[$name]=$add
echo "$name - ${address[$name]}"
done < adresses.txt
for name in ${!address[*]}
do
echo "$name - ${address[$name]}"
done
The script work properly. However, in the FOR loop, i'm having some problems when the person name has spaces (For example "John Nobody"). How can I fix this?
You need to use more quotes to maintain the values with whitespace as "words":
declare -A array
while IFS='|' read -r name value; do
array["$name"]="$value"
done <<END
foo bar|baz
jane doe|qux
END
for key in "${!array[@]}"; do echo "$key -> ${array[$key]}"; done
# .........^............^ these quotes fix your error.
foo bar -> baz
jane doe -> qux
The quotes in "${!array[@]}"
in the for
loop mean that the loop iterates over the actual elements of the array. Failure to use the quotes means the loop iterates over all the individual whitespace-separated words in the value of the array keys.
Without the quotes you get:
for key in ${!array[@]}; do echo "$key -> ${array[$key]}"; done
foo ->
bar ->
jane ->
doe ->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With