Creating Matrix from file

Hi all, I'm a newbie in shell scripting and currently I'm trying to create a matrix using bash. The Output will look like this

               AB   CDE   FG
 1   
2   
3
4
5
6
7

I'm stuck on the ABCDEFG display.

printFlightSeats()
{

rows=7
columns=7
for ((i=0;i<=$rows;i++))
do
    echo -n "$i" | tr "0" " "
    for ((j=0;j<$columns;j++))
    do  
    echo -n "$j" | tr "0" " " | tr "1234567" "ABCDEFG"
        
    done
    printf $j
    printf "$i\n"
done

}

Can anyone advise me? Thanks in advance!:slight_smile:

---------- Post updated at 05:48 PM ---------- Previous update was at 05:44 PM ----------

Sorry guys the output should be

     AB CDE FG
1
2
3
4
5
6
7

I've never been a fan of echo | some-command when it's not needed as it is very inefficient. This is a simple, yet hard coded approach that generates output exactly as you indicated:

nrows=${1:-7}


for col in "  " A B "  " C D E "  " F G    # print header row
do
       printf "%s" "$col"
done
printf "\n"

for $(( row=1; row < $nrows; row++ ))    # print rows
do
    printf "%s\n" "$row"
 done

EDIT: The first loop could be omitted and a single printf used. Initially I embedded the loop inside of the row loop, but realised that the output you wanted didn't show the 'seats' on any row but the header, and I just moved the code up without thinking about the fact that the loop was then not needed.

 The code below is a bit more complex, but much more flexible assuming that the different aircraft configurations might need to be supported.  The looping is also different to illustrate how you might fill in any kind of status for each seat \(assigned, open, frequent-flier only, ,etc\):
nrows=${1:-7}
ncols=${2:-7}
typeset -a layout
case $ncols in
    2) layout=(A " "  B );;
    3) layout=(A "  " C D);;
    4) layout=(A B "  " C D);;
    6) layout=(A B C "  " D E F);;
    7) layout=(A B " "  C D E " "  F G);
     9) layout=(A B "  " C D E F G "  " H I);;
    *)  echo "invalid layout size entered"
        exit 1
        ;;
esac

for row in " " $( seq 1  $nrows )
do
    printf "%s  " "$row"
    for (( col=0; $col < ${#layout[@]}; col++ ))
    do
        printf "%s" "${layout[$col]}"
    done
    printf "\n"
done
exit

 

The number of rows and number of seats/row are entered on the command line; output from this script is like this:

>>>show_seats 6 7
   AB CDE FG
1  AB CDE FG
2  AB CDE FG
3  AB CDE FG
4  AB CDE FG
5  AB CDE FG
6  AB CDE FG

1 Like

Thanks alot for your response!

For this code,

nrows=${1:-7}

Is {1:-7} the syntax for declaring values like "1-7"?

I've done some research on the codes your provided.

typeset -a layout
case $ncols in
    2) layout=(A " "  B );;
    3) layout=(A "  " C D);;
    4) layout=(A B "  " C D);;
    6) layout=(A B C "  " D E F);;
    7) layout=(A B " "  C D E " "  F G);;
    8) layout=(A B "  " C D E F G "  " H I);;
    *)  echo "invalid layout size entered"
        exit 1
        ;;
esac

For the first line it actually creates a variable "layout" indicating that "each name is an array variable" by using "-a".
Thus I assume in your "case" you were creating the array for the "seats" display.
But why is there a need for "case 8)" since I only need to display "AB CDE FG"?

I'm trying to retrieve the "seats" display from another flat file with a delimiter of "," to determine which seat is available.
The flat file data looks like this:
A1,A2,B3,B4,E4 etc.

I've tried using "grep" to retrieve the whole line and using "sed" to translate each value to "X" as an indication. However I'm stuck at how do I store the index for each value in my flat file so that it will be able to display correctly in my matrix.