0

I want to transpose a file using a bash script, but I'm having issues with echoing multidimensional arrays.

file.txt has the following content:

name age
alice 21
ryan 30

Output the following:

name alice ryan
age 21 30

code

file_content=$(cat file.txt)
parent_array=()
while IFS= read -r line; do
    IFS=' ' read -r -a split_array <<< "$line"
    elements_len=${#split_array[@]}
    for ((i=0; i<$elements_len; i++)); do
       parent_array[$i]+="${split_array[$i]} "
    done
done <<< "$file_content"
# Iterate over the multi-dimensional array
for ((i=0; i<${#parent_array[@]}; i++)); do
  for ((j=0; j<${#parent_array[$i]}; j++)); do
    echo "${parent_array[$i][$j]}"
  done
done

error

$ bash transpose.sh
transpose.sh: line 13: ${parent_array[$i][$j]}: bad substitution

I tried to transpose the file 'file.txt' using a Bash script. I stored the content of 'file.txt' in a multidimensional array, but I am facing issues when trying to echo the elements. I would like to know how to iterate through this type of array in a Bash script.

Also, how can I remove the trailing space only for the last echo statement? I have attempted the following code, which prints all elements, but I am unable to skip the trailing space for the last echo at each index level:

for element in "${parent_array[@]}"; do
    echo "$element"
done

This loop works for multidimensional arrays, but when echoing, it outputs the name 'name alice ryan ' instead of the desired 'name alice ryan'. How can I remove the trailing space only for the last echo statement? I posted this question on Stack Overflow. Please correct any grammatical mistakes and provide suggestions for improvement.

suresh
  • 11
  • 2
  • 2
    Please add a valid shebang (`#!/bin/bash`) to your script, then paste it at http://www.shellcheck.net/. – Cyrus Jun 11 '23 at 00:37
  • 2
    Bash doesn't support multi-dimensional arrays. See [How to declare 2D array in bash](https://stackoverflow.com/q/16487258/4154375) and [Multi-dimensional arrays in Bash](https://stackoverflow.com/q/11233825/4154375) for possible workarounds. You should consider using Python, or some other fully-featured programming language, for a problem like this though. – pjh Jun 11 '23 at 01:27
  • 1
    Also see [An efficient way to transpose a file in Bash](https://stackoverflow.com/q/1729824/4154375) and [Transposing rows and columns](https://unix.stackexchange.com/q/79642/264812). – pjh Jun 11 '23 at 01:36
  • This is not a job for shell - see [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice). If you want to do this using mandatory POSIX tools then use awk. – Ed Morton Jun 11 '23 at 12:06
  • `infile=file.txt; fields=(); row_idxs=(); ncols=-1; while read -r -a cols || (( ${#cols[*]} > 0 )); do (( ncols == -1 )) && ncols=${#cols[*]}; row_idxs+=( "${#fields[*]}" ); fields+=( "${cols[@]}" ); done <"$infile"; for (( c=0; c – pjh Jun 11 '23 at 14:03

0 Answers0