Perl- creating a matrix from a 3 column file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl- creating a matrix from a 3 column file
# 1  
Old 11-13-2012
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 third column in each right position. As follows:
My input look like:
Code:
aa 3 5
cc 9 11
bb 15 7
aa 12 5
cc 18 11
bb 6 7

And I need to have an output like this:
Code:
aa bb cc
3 5       
6   7
9     11
12 5    
15   7
18     11

Hope to be clear...
thank you for any help!!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by gabrysfe; 11-13-2012 at 01:14 PM.. Reason: code tags, please!
# 2  
Old 11-13-2012
Like this?
Code:
perl -ane '$val1{$F[0]}{$F[1]}=$F[2];$val2{$F[1]}{$F[0]}=$F[2];
END{
$"="\t";
@h=sort keys %val1;
print " ", $", "@h\n";
for $y (sort {$a<=>$b} keys %val2) {
 print "$y",$";
 for $x (@h) {
  print "$val1{$x}{$y}",$";
 }
 print "\n";
}
}' file


Last edited by elixir_sinari; 11-15-2012 at 04:54 AM..
# 3  
Old 11-14-2012
I'm trying it...but doesn't work properly Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Convert a 3 column tab delimited file to a matrix

Hi all, I have a 3 columns input file like this: CPLX9PC-4943 CPLX9PC-4943 1 CPLX9PC-4943 CpxID123 0 CPLX9PC-4943 CpxID126 0 CPLX9PC-4943 CPLX9PC-5763 0.5 CPLX9PC-4943 CpxID13 0 CPLX9PC-4943 CPLX9PC-6163 0 CPLX9PC-4943 CPLX9PC-6164 0.04... (7 Replies)
Discussion started by: AshwaniSharma09
7 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

3 column .csv --> correlation matrix; awk, perl?

Greetings, salutations. I have a 3 column csv file with ~13 million rows and I would like to generate a correlation matrix. Interestingly, you all previously provided a solution to the inverse of this problem. Thread title: "awk? adjacency matrix to adjacency list / correlation matrix to list"... (6 Replies)
Discussion started by: R3353
6 Replies

5. Shell Programming and Scripting

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 (2 Replies)
Discussion started by: vinzping
2 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. Shell Programming and Scripting

Extracting columns from a matrix and storing each column in a separate file

Hi All, I have a huge matrix file consisting some some millions rows and 6000 columns. The contents are just floating point numbers in the matrix. I want to extract each column (i.e. 6000 of them) and store each column in a separate file. For example, 1.dat will consist of elements from column... (4 Replies)
Discussion started by: shoaibjameel123
4 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. UNIX for Dummies Questions & Answers

creating a file using the fist column and printing second column

Hello all. I have a problem that I need help solving. I would like to convert the following file: human pool1_12 10e-02 45 67 human pool1_1899 10e-01 45 29 human pool1_1829 10e-01 43 26 horse pool1_343 10e-20 65 191 horse pool1_454 10e-09 44 43... (5 Replies)
Discussion started by: viralnerd
5 Replies
Login or Register to Ask a Question