Matrix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matrix
# 1  
Old 01-25-2011
Matrix

Hi All

I would like to merge multiple files with the same row and column size into a matrix format

In a folder I have multiple files in the following format
Code:
vi 12.txt
a 1
b 5
c 7
d 0

vi 45.txt
a 3
b 6
c 9
d 2

vi 9.txt
a 4
b 7
c 4
d 3

vi 76.txt
a 3
b 6
c 46
d 75

Like this 1000's of files

Notice that all files have the same first column.
I want to merge all the files into a single file with the following format
Code:
  12 45 9 76
a  1  3  4  3
b  5  6  7  6
c  7  9  4  46
d  0  2  3  75

Where a top row is also added which is the first part (number before.txt) of the files name

Is it a good way to do it using awk or sed.

LA

---------- Post updated at 03:23 PM ---------- Previous update was at 12:54 PM ----------

Please let me know to do such large merging in an effective way either used awk or sed
# 2  
Old 01-25-2011
Seems like a job for cut and paste

cut to extract the 2nd column for each file after the first.
paste to combine the files across each row.

Something like:
Code:
$ cut -d' ' -f2 <m45.txt >m45a.txt

$ paste m12.txt m45a.txt | awk '{print $0}'
a 1     3
b 5     6
c 7     9
d 0     2

# 3  
Old 01-25-2011
I have thousands of such files
# 4  
Old 01-25-2011
nawk -f lucky.awk myFilesGoHere
lucky.awk:
Code:
FNR==1 {
        file=substr(FILENAME,1,index(FILENAME,".")-1)
        head=(FNR==NR)?file: head OFS file
}
FNR==NR {row[FNR]=$1}
{
  mat[FNR]=(FNR==NR)?$2:mat[FNR] OFS $2
}
END {
  print OFS,head
  for(i=1;i in mat; i++)
     print row[i] OFS mat[i]
}

# 5  
Old 01-27-2011
Thanks,
I have question in the above code:

Do I need to type in all the files or point it to the directory containing the files at myFilesGoHere.

Please let me know.
LA
# 6  
Old 01-27-2011
Code:
( filenrs="12 45 9 76"
  echo "  $filenrs" 
  set -- $filenrs
  join $1.txt $2.txt | join - $3.txt | join - $4.txt
) > file.out

# 7  
Old 01-27-2011
Quote:
Originally Posted by Lucky Ali
Thanks,
I have question in the above code:

Do I need to type in all the files or point it to the directory containing the files at myFilesGoHere.

Please let me know.
LA
No. If there are only these txt files under currently folder, you try vgersh99's code with *.txt as myFilesGoHere

Code:
awk -f lucky.awk *.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

List to matrix

I want to go from 1,a,1a 1,b,1b 1,c,1c 2,a,2a 2,b,2b 3,a,3a to a,b,c 1,1a,1b,1c 2,2a,2b,- 3,3a,-,- Here is what I tried awk -F, 'BEGIN {OFS = ","} (4 Replies)
Discussion started by: senhia83
4 Replies

2. Shell Programming and Scripting

MATRIX to CSV

Hello friends, A big question for the UNIX INTELLIGENCE I have a CSV file as follows: VALUE,USER1,relatedUSER1,relatedUSER2 -1,userA,userB,userC 1,userN,userD,userB 0,userF,userH,userG 0,userT,userH,userB 1,userN,userB,userA -1,userA,userF,userC 0,userF,userH,userG... (15 Replies)
Discussion started by: kraterions
15 Replies

3. Shell Programming and Scripting

column to matrix

Hello All, I need your help in the following problem. I have a matrix of 500 columns and 1000 rows and in each cell, it is having a value range from 0 to 9. I would like to convert each column in to a matrix, according to the value in each cell (ie) 0 to 9. For each column, I need a matrix... (5 Replies)
Discussion started by: Fredrick
5 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. Shell Programming and Scripting

awk? adjacency matrix to adjacency list / correlation matrix to list

Hi everyone I am very new at awk but think that that might be the best strategy for this. I have a matrix very similar to a correlation matrix and in practical terms I need to convert it into a list containing the values from the matrix (one value per line) with the first field of the line (row... (5 Replies)
Discussion started by: stonemonkey
5 Replies

6. Programming

Matrix code in C++

I have the code below in C++ and am trying to understand what these do and how to call them. template <class Type> class Matrix { public: Matrix(); Matrix( const int m, const int n ); Matrix( const Matrix& A ); ~Matrix(); (0 Replies)
Discussion started by: kristinu
0 Replies

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

8. Shell Programming and Scripting

Matrix construction

Hi experts How to construct a rectangular matrix for a text file with 6012 rows and 2221 columns. Thank You (1 Reply)
Discussion started by: riyabio
1 Replies

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