If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > transposing rows and columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-04, 09:09
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Exclamation transposing rows and columns

Hi all.

I am having a bad time with this problem. Could someone
shed some light, please ?

My program saves N numbers in a single line (usually there are more
than 400 numbers on it). This is done M times, resulting in a matrix
like the one below :

1 4 10 -1 3 7 ...
20 1 44 2 -10 5 ...
2 5 50 2 0 1 ...
...
...

How can I transpose this NxM matrix to MxN ?

1 20 2
4 1 5
10 44 50
-1 2 2
3 -10 0
7 5 1
Thanks,

Serg
Reply With Quote
  #2 (permalink)  
Old 05-20-04, 22:36
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
nawk -f serg.awk file.txt

Code:
# with this infile:
#
# a b c d
# e f g h
# i j k l
#
{for(i=1;i<=NF;i++){a[i,NR]=$i};m=NF;n=NR}
END{for(j=1;j<=m;j++){
         for(k=1;k<=n;k++){printf("%s ",a[j,k])}print ""}}

# outputs:
#
#
# a e i
# b f j
# c g k
# d h l
Reply With Quote
  #3 (permalink)  
Old 05-21-04, 09:35
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Thanks for the reply.

It worked like a charm.

Serg
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On