Home > other >  Combine the array of strings in Bash
Combine the array of strings in Bash

Time:10-25

I have:

TEXT='1,2
a,b,c
XX,YY'

I need the output like this:

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.

CodePudding user response:

A solution using brace expansion and eval (aka evil) might be:

eval printf -v OUT "%s\\\n" {${TEXT//$'\n'/'},{'}}
OUT=${OUT%?} # to strip off the trailing newline character
echo "$OUT"

CodePudding user response:

#!/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

CodePudding user response:

One awk using a recursive function call:

awk '
BEGIN { FS=OFS="," }

      { for (i=1;i<=NF;i  ) {
            val[NR,i]=$i              # val[row#][element#]=elementVal
            max[NR]=NF                # number of elements in a row
        }
      }

function print_tuples(row, out, i) {

    if (row > NR)
        print out
    else
        for (i=1;i<=max[row];i  )
            print_tuples(row 1, out (out ? OFS : "") val[row,i])
}

END   { print_tuples(1) }
' <<< "${TEXT}"

This generates:

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

Storing in bash variable OUT:

$ OUT=$(awk '... see code above ...')
$ typeset -p OUT
declare -- 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"
  • Related