Issue
I have:
TEXT='1,2
a,b,c
XX,YY'
OUT='1,a,XX
1,a,YY
1,b,XX
1,b,YY
1,c,XX
1,c,YY
2,a,XX
2,a,YY
2,b,XX
2,b,YY
2,c,XX
2,c,YY'
Should be some recursion, because the length of lines and lines count is unknown. Explanation would be great, but just the code is also OK.
What I have done so far (trying to use arrays):
TEXT='1,2
a,b,c
XX,YY'
declare -a lines
COUNT=0
while read LINE; do
lines[$COUNT]=$LINE
COUNT=$((COUNT+1))
done <<<"$TEXT"
declare -A A
K1_MAX=0
K2_MAX=0
for key in "${!lines[@]}"; do
IFS=',' read -ra symbols <<<"${lines[$key]}"
for key2 in "${!symbols[@]}"; do
if [[ $key -gt $K1_MAX ]]; then K1_MAX=$key; fi
if [[ $key2 -gt $K2_MAX ]]; then K2_MAX=$key2; fi
A[$key $key2]=${symbols[$key2]}
done
done
for key in "${!A[@]}"; do
echo $key: "${A[$key]}"
true
done | sort
This gives me the output:
0 0: 1
0 1: 2
1 0: a
1 1: b
1 2: c
2 0: XX
2 1: YY
But still can't understand how to combine those values between themselves.
Solution
#!/bin/bash
TEXT='1,2
a,b,c
XX,YY'
buildstr(){
local prefix="$1"
if [ -z "$2" ]; then
printf "%s\n" "$prefix"
else
IFS=, read -r -a a <<<"$2"
shift 2
for i in "${a[@]}"; do
buildstr "${prefix:+$prefix,}$i" "$@"
done
fi
}
readarray -t a <<<"$TEXT"
OUT=$(buildstr "" "${a[@]}")
declare -p OUT
Answered By - jhnc Answer Checked By - Terry (WPSolving Volunteer)