Read in Table as a matrix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read in Table as a matrix
# 1  
Old 06-27-2012
Read in Table as a matrix

Dear forum users,
i'm trying to read a table with 40x122 data in a array. Following this, i'd plot each rows again the header of the file in gnuplot.

i was thinking for something like that
Code:
#!/bin/bash 
# reads from the $ips file and assigns to $MYARRAY
#IFS =";" split the line after the ;
#set -- $array save the each row of the line in $1 $2 $3 $4 ....

myfile="Comprasion.txt"
ln=`wc -l $myfile|awk '{print $1}'`
i=1
#echo "$ln"
declare -a sgs
for ((i==1; i<=$ln; i++)); do 
         line=`awk "NR==$i" $myfile`
         #echo "$line"
         declare -a row
         linarray=(`echo ${line// / }`);
         #echo "${#linarray[*]}" # OK.
         #echo "${#linarray[*]}" # OK.
         row=(`echo ${#linarray[*]}`);
         echo "*********"
         #echo "$row"
         r=1
         for ((r==1;r<=$row;r++)); do 
            #echo "variable $i $r"; # OK.
            sgs[${i},${r}]=(`echo ${linearray[$r]}`);
            var=${sgs[${i},${r}]};
         echo "$var";
         done
         #echo "$i";
done

if i run the script, unfortunately, i get nothing. Where do i have problem?

thanks in advance for your help

---------- Post updated at 07:26 PM ---------- Previous update was at 03:43 PM ----------

nobody has a suggestion Smilie

Last edited by servuskelb; 06-27-2012 at 04:03 PM.. Reason: code improved
# 2  
Old 06-27-2012
Please post sample input and an example of the desired/expected output.
# 3  
Old 06-27-2012
here is a part of the input file
Code:
  Lon    Lat      1990    1991   1992   1993
-10.75  51.25    58.0    51.0    52.6    55.0 #st1
-9.25  40.75    58.0    51.0    52.6    55.0   #st2
-8.25  54.25    74.5    82.2    67.6    67.8   #st3
-7.25  51.75    57.0    55.6    54.8    54.8   #st4
-7.25  51.75    57.0    55.6    54.8    54.8   #st5
-7.25  54.25    79.2    85.2    68.6    74.8    #st6
-6.75  52.75    58.5    66.8    60.8    63.6    #st7
-1.25  50.75    66.5    70.2    63.0    57.0    #st8
-0.75  48.75    67.5    70.0    64.8    58.4    #st9
2.75  50.75    79.5    82.8    74.4    63.2      #st10

i'd like to plot the years on x-axis and the value of st[1..10] on the y-axis with line via gnuplot. Therefore, i need to split the table in a 2 dimensional array as sgs(year,st1..10)

Last edited by Franklin52; 06-28-2012 at 04:38 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 06-28-2012
Could you also post an example of the expected output? How the data to be passed to GNU plot should look like?
# 5  
Old 06-28-2012
Hi radoulov,
the output should look like:
Code:
1990 58.0
1991 51.0
1992 52.6
1993 55.0
e
1990 58.0
1991 51.0
1992 52.6
1993 55.0
e
1990 74.5
1991 82.2
1992 67.6
1993 67.8
e
end etc.

thank you very much for your help
# 6  
Old 06-28-2012
Assuming the last column (values with a leading #) is not present in the real input:

Code:
awk 'NR == 1 {
  n = split($0, y)
  next
  }
{
  for (i = 2; ++i <= NF;)
    print y[i], $i
  print "e"
  }' infile

Otherwise:
Code:
awk 'NR == 1 {
  n = split($0, y)
  next
  }
{
  for (i = 2; ++i < NF;)
    print y[i], $i
  print "e"
  }' infile

# 7  
Old 06-28-2012
Thank you very much it works perfekt
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read Table in file and split

HELLO I need your help please , i need to read a file that contain a table like : Name | Status ------------------ DB 1 | UP DB 2 | UP DB 3 | DOWN DB 4 | UP DB 5 | UP the objective to read each line and check if DB is UP or Down and give me the name of Down database.... (10 Replies)
Discussion started by: Abdellah
10 Replies

2. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

3. Shell Programming and Scripting

How to sort matrix table in UNIX?

Hello All, i have a file sort.txt with below entries. 1 12 10 16 6 4 20 8 15 i need to sort these entries and the out put should come in a single line. 1 4 6 8 10 12 15 16 20 Can you please help me sort this out? (2 Replies)
Discussion started by: sureshk_85
2 Replies

4. Shell Programming and Scripting

Table to Matrix

Hi, I have a table in the format: 1 0 -1 1 0 2 0 1 -1 0 0 0 3 0 1 1 0 0 0 0 0 0 etc. I am trying to input this to a program, however it is complaining about the fact that it is not in matrix format. How do I add 0's to end of the rows to make them even? Thanks in advance! (2 Replies)
Discussion started by: Rhavin
2 Replies

5. HP-UX

how to read routing table

Hi all, Could someone please explain to me how I should read this routing table, # netstat -rn Routing tables Destination Gateway Flags Refs Interface Pmtu 127.0.0.1 127.0.0.1 UH 0 lo0 32808 10.222.47.82 10.222.47.82 UH... (3 Replies)
Discussion started by: rachael
3 Replies

6. UNIX for Dummies Questions & Answers

Teradate table read?

i have to read some data from a teradata table and use them in my shell script.. how do i do that.. we can use BTEQ .. but i don know how to pass the field values as variables to the shell.. please help me.... (8 Replies)
Discussion started by: depakjan
8 Replies

7. Shell Programming and Scripting

Is it possible to draw table/matrix using shell script?

Hi all, I need to create a matrix of variable rows and columns. Right now i have 3 rows and two columns and following values. Output something like TypeA TypeB TestCase1 Pass Fail TestCase2 Pass ... (2 Replies)
Discussion started by: jakSun8
2 Replies

8. Solaris

Need some lib in c/c++ to read db table at one go

Hi All, I need some sort of library or module which read the db table at one go and keep in process memory.My problem is that I have a call processing module which handles the network call and goes for db lookup for subscriber profile in oracle db for every call.I am using Oracle Standard Edition... (0 Replies)
Discussion started by: unisuraj
0 Replies
Login or Register to Ask a Question