I have a file which contains the single line below:
{machineA=[0, 1024, 4, 1028], machineB=[1, 1025, 5, 1029]}
in which I have two datasets:
machineA=[0, 1024, 4, 1028]
machineB=[1, 1025, 5, 1029]
Now, I need to read the above file and split it in such a way that I can extract each machine's information as mentioned above and store it in some data structure.
Currently, I am confused which data structure I should be using in a bash shell script. If I was doing this in Java, I would use Map<String, Set<String>>
but I'm not sure what should I use in a shell script.
And after storing it in some data structure, I need to iterate it and print out the result.
I am able to read the above file using the shell script below:
#!/bin/bash
while read -r line; do
echo "$line"
done < data.txt
But I am not sure how to split the above line data in such a way so that I am able to extract each machine informaiton and then store it in some data structure?
UPDATE:-
Below is my shell script I have got after following the suggestion given by glenn -
#!/bin/bash
while read -r line; do
echo "$line"
declare -A "$(
echo "x=("
grep -oP '(\w+)(?==)|(?<==\[).*?(?=\])' <<< "$line" |
tr -d , |
paste - - |
sed 's/^/[/; s/\t/]="/; s/$/"/'
echo ")"
)"
for key in "${!x[@]}"; do # need quotes here
for element in ${x[$key]}; do # no quotes here
printf "%s\t%s\n" "$key" "$element"
done
done
done < primary.txt