Creating Matrix from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating Matrix from file
# 1  
Old 11-06-2011
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

Code:
               AB   CDE   FG
 1   
2   
3
4
5
6
7

I'm stuck on the ABCDEFG display.

Code:
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!Smilie

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

Sorry guys the output should be

Code:
     AB CDE FG
1
2
3
4
5
6
7

Moderator's Comments:
Mod Comment Video Tutorial on how to use Code Tags in the UNIX and Linux Forums

Last edited by pludi; 11-06-2011 at 06:32 AM.. Reason: Some mistakes in displaying
# 2  
Old 11-06-2011
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:


Code:
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):


Code:
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:


Code:
>>>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


Last edited by agama; 11-06-2011 at 01:40 PM.. Reason: clarification
This User Gave Thanks to agama For This Post:
# 3  
Old 11-06-2011
Thanks alot for your response!

For this code,
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.
Code:
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.

Last edited by vinzping; 11-06-2011 at 09:07 PM.. Reason: For a better display on the codings
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating a matrix out of a longitudinal data set

Hi I do have a tab delimited file with 2 columns, which is stratified based on the first column. There are 1000's of values in the file. Below is an example of the input file 1 AB 1 AC 1 CC 1 DD 2 AB 2 CC 2 AC 2 AB 3 CF 3 CC 3 DD 4 AC 4 CC 4 AD (5 Replies)
Discussion started by: Kanja
5 Replies

2. Programming

C++: Creating Matrix template using vector

I want to create a Matrix template that uses vector. For the time being I want to create the following operations. I need setting the implementation for the operations. Maybe I do not have to use a pointer either. template <class T> class Matrix { protected: typedef vector<T>* ... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

Creating matrix from folders and subfolders

Hello, Greetings! please help me produce the following solution. I need to produce one big matrix file from several files in different levels. If it helps, the index folder provides information on chromosome index and the data folder provides information on values for chromosomes. there... (8 Replies)
Discussion started by: newbie83
8 Replies

4. Shell Programming and Scripting

Perl- creating a matrix from a 3 column file

Dear all, I'm new in perl scripting and I'm trying to creating a matrix from a 3 column file sorting data in a particular manner. In the final matrix I need to have the first column "IDs" on the header of the columns and the second column values on the header of each row. And the value fo the... (2 Replies)
Discussion started by: gabrysfe
2 Replies

5. Shell Programming and Scripting

Converting to matrix-like file using AWK

Hi, Needs for statistics, doing converting Here is a sample file Input : 1|A|17,94 1|B|22,59 1|C|56,93 2|A|63,71 2|C|23,92 5|B|19,49 5|C|67,58 expecting something like that Output : 1|A|17,94|B|22,59|C|56,93 2|A|63,71|B|0|C|23,92 5|A|0|B|19,49|C|67,58 (11 Replies)
Discussion started by: fastlane3000
11 Replies

6. UNIX for Dummies Questions & Answers

BASH - Creating a Matrix

I'm trying to create a Matrix using bash. The expected output is .AB CDE FG 1 2 3 4 5 6 7 I'm a newbie in shell language, really appreciate if there is anyone who can guide me with this. Double post again, continued here (0 Replies)
Discussion started by: vinzping
0 Replies

7. Ubuntu

Creating Matrix

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 (0 Replies)
Discussion started by: vinzping
0 Replies

8. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

9. Shell Programming and Scripting

Creating a matrix from files.

I need to create a large matrix so that I can feed that matrix to MATLAB for processing. The problem is creating that matrix because my data is completely scattered around files. 1. I have one big dictionary file which has words in newlines, like apple orange pineapple 2. I have some... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

10. Shell Programming and Scripting

diagonal matrix to square matrix

Hello, all! I am struggling with a short script to read a diagonal matrix for later retrieval. 1.000 0.234 0.435 0.123 0.012 0.102 0.325 0.412 0.087 0.098 1.000 0.111 0.412 0.115 0.058 0.091 0.190 0.045 0.058 1.000 0.205 0.542 0.335 0.054 0.117 0.203 0.125 1.000 0.587 0.159 0.357... (11 Replies)
Discussion started by: yifangt
11 Replies
Login or Register to Ask a Question