mardi 24 mars 2015

Read file remove spaces and store in array


I have a file with the following content:


list:



blue,none
red,none
orange,plot
baseball , none
university,none
school,none
desk,plot
monitor,none
earphone,none


I need to read this file, remove spaces and store each column in a different array.


script:



output_names=()
output_plots=()

while read line
do
item=$(echo -e "${line}" | tr -d '[[:space:]]')
item=$(echo $item | tr "," "\n")
output_names+=(${item[0]});
output_plots+=(${item[1]});
done <list

echo "** output_names:";
for item in ${output_names[*]}
do
echo $item
done

echo "** output_plots:";
for item in ${output_plots[*]}
do
echo $item
done


However it does not work as I expect. What is wrong? and how to fix this code?


Note


If somebody has a solution to store the data in a single array with different keys output['names'][*] and output['plots'][*], that would be highly appreciated as I do not know how to do it.


Outputs:



** output_names:
blue
none
red
none
orange
plot
baseball
none
university
none
school
none
desk
plot
monitor
none
earphone
none
** output_plots:


Aucun commentaire:

Enregistrer un commentaire