transposing square matrixs or blocks in a big file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting transposing square matrixs or blocks in a big file
# 1  
Old 02-01-2011
transposing square matrixs or blocks in a big file

Hi

I do have a big file of the following format
Code:
  a b c d e f g
  2 3 5 6 6 6 7
  3 4 5 6 7 9 0
  4 5 7 8 9 9 0
  1 2 4 5 6 7 8
  3 5 6 7 2 3 4
  5 6 7 4 3 2 4
  5 4 5 6 3 5 5 
  r h i j k l m 
  2 3 4 5 6 7 8
  4 5 7 8 9 9 0
  3 5 6 7 2 3 4
  2 3 5 6 6 6 7
  5 5 7 8 9 2 3
  1 2 4 5 6 7 8
  3 5 6 7 2 3 4

Basically the data is in a matrix format with many square matrixes put together. The bold fonts represents the beginning of such square matrixes. I have shown only 2 such blocks here but I have 100's of such blocks in that file in tab delimited format.

I want to to a transpose (change the axes) of each of the this square matrixes and get a tab delimted file as shown below.
Code:
a 2 3 4 1 3 5 5
b 3 4 5 2 5 6 4
c 5 5 7 4 6 7 5
d 6 6 8 5 7 4 6
e 6 7 9 6 2 3 3
f 6 9 9 7 3 2 5
g 7 0 0 8 4 4 5
r 2 4 3 2 5 1 3
h 3 5 5 3 5 2 5
i 4 7 6 5 7 4 6
j 5 8 7 6 8 5 7
k 6 9 2 6 9 6 2
l 7 9 3 6 2 7 3
m 8 0 4 7 3 8 4

Please let me know the best way to do it eith in sed or awk.

LA

---------- Post updated at 05:03 PM ---------- Previous update was at 03:39 PM ----------

Please let me know if there is a way to do this
# 2  
Old 02-01-2011
Do the headers only contain letters and the fields only numbers?
# 3  
Old 02-01-2011
The letters contains both letters and numbers (like letters_numbers) but the fields are always numbers.

Hope this helps.
LA

---------- Post updated at 05:17 PM ---------- Previous update was at 05:16 PM ----------

Sorry a typo

The heads (not letters) contains both letters and numbers (like letters_numbers) but the fields are always numbers.
# 4  
Old 02-01-2011
You can just transpose the small blocks and print and then get the new block, not very memory intensive. What have you tried so far?
# 5  
Old 02-01-2011
try:
Code:
 
 awk '
{if(/[a-z]/){x++}
{for(i=1;i<=NF;i++) a[x,NR,i]=$i;t=NR;s=NF
}}
END {for(l=1;l<=x;l++)
{for(m=1;m<=s;m++)
{for(n=1;n<=t;n++) {printf (length(a[l,n,m])>0?a[l,n,m]" ":"")}
{print ""}}}
}' urfile

This User Gave Thanks to yinyuemi For This Post:
# 6  
Old 02-01-2011
I can transpose block by block but there is like 800 blocks stagged in the file. I don't know how to loop it through.

---------- Post updated at 05:42 PM ---------- Previous update was at 05:32 PM ----------

Hi yinyuemi,

I tested with ur suggestion but its doing it only for the first 2 columns of each block (square matrix). If we could extend it to the 5 more columns in each block that would solve the problem.
# 7  
Old 02-01-2011
Try this, which works as described earlier:
Code:
awk 'function pm(){
       for(j=1;j<=NF;j++)
         for(i=1;i<=m;i++)
           printf "%s"(i<m?OFS:RS),M[j,i]
     }
     /[[:alpha:]]/{
       if(m)pm()
       m=0
     }
     {
       ++m
       for(i=1;i<=NF;i++)
         M[i,m]=$i
     }
     END{
       pm()
     }' infile


Last edited by Scrutinizer; 02-01-2011 at 07:27 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Row blocks to column blocks

Hello, Searched for a while and found some "line-to-column" script. My case is similar but with multiple fields each row: S02 Length Per S02 7043 3.864 S02 54477 29.89 S02 104841 57.52 S03 Length Per S03 1150 0.835 S03 1321 0.96 S03 ... (9 Replies)
Discussion started by: yifangt
9 Replies

2. Shell Programming and Scripting

Difficult transposing of data from profiles blocks

Hello to all, I really hope some expert or awk guru could help me with this. I don't have how to begin and hope is not so difficult for somebody. I'll expecting how someone could resolve this problem I have to parse this. I have blocks of parameters for each MSISDN and I would like to extract... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

3. Shell Programming and Scripting

Transposing a file

Hi Guys, I have file containing this kind of format below: ======== MOBILITY EVENT (G): ATTACH REJECT ========= Time : <date_time> Node : <node> GMM Cause : <code> Details : <details> Attach : <attach type> IMSI : <imsi> PTMSI : <ptmsi> RA New : <ra new> RA... (9 Replies)
Discussion started by: rymnd_12345
9 Replies

4. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

5. UNIX for Dummies Questions & Answers

Convert 512-blocks to 4k blocks

I'm Unix. I'm looking at "df" on Unix now and below is an example. It's lists the filesystems out in 512-blocks, I need this in 4k blocks. Is there a way to do this in Unix or do I manually convert and how? So for container 1 there is 7,340,032 in size in 512-blocks. What would the 4k block be... (2 Replies)
Discussion started by: rockycj
2 Replies

6. UNIX for Dummies Questions & Answers

How big is too big a config.log file?

I have a 5000 line config.log file with several "maybe" errors. Any reccomendations on finding solvable problems? (2 Replies)
Discussion started by: NeedLotsofHelp
2 Replies

7. Shell Programming and Scripting

Transposing a file

Hi All, I have a input file say FILEA. FILEA -------- empid1 sal1 location1 manager1 empid2 sal2 location2 manager2 empid3 sal3 location3 manager3 . . . (3 Replies)
Discussion started by: 46019
3 Replies

8. Shell Programming and Scripting

Transposing a file

i have a file as: 1 2 3 4 5 i want output as : 1 2 3 4 5 can anybody help on this?? (14 Replies)
Discussion started by: vikas_kesarwani
14 Replies

9. Shell Programming and Scripting

file transposing

Hello, Is there a way to transpose a file in shell scripting? For instance, from a1 a2 a3 a4 a5 a6 a7 .... b1 b2 b3 b4 b5 b6 b7 .... c1 c2 c3 c4 c5 c6 c7 .... d1 d2 d3 d4 d5 d6 d7 ... ... ... ... to a1 b1 c1 d1 .... a2 b2 c2 d2 .... a3 b3 c3 d3 .... a4 b3 c3 d4 .... ... ... (24 Replies)
Discussion started by: mskcc
24 Replies

10. UNIX for Dummies Questions & Answers

How to view a big file(143M big)

1 . Thanks everyone who read the post first. 2 . I have a log file which size is 143M , I can not use vi open it .I can not use xedit open it too. How to view it ? If I want to view 200-300 ,how can I implement it 3 . Thanks (3 Replies)
Discussion started by: chenhao_no1
3 Replies
Login or Register to Ask a Question