The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to transpose data elements in awk ahjiefreak Shell Programming and Scripting 2 05-13-2008 05:44 AM
outputting data in table from grep results Chillspark Shell Programming and Scripting 2 05-12-2008 03:21 PM
Help Inserting data in mysql table vadharah Shell Programming and Scripting 11 03-30-2008 01:26 PM
Comparing data in file with values in table Mohit623 Shell Programming and Scripting 0 01-22-2008 08:57 AM
ccall database and collect data from one table rinku Shell Programming and Scripting 0 05-28-2007 02:16 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-28-2008
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
How to transpose a table of data using awk

Hi.

I have this data below:-
v1 28 14 1.72414 1.72414 1.72414 1.72414 1.72414
v2 77 7 7.47126 6.89655 6.89655 6.89655 6.89655
v3 156 3 21.2644 21.2644 20.6897 21.2644 20.6897
v4 39 3 1.72414 1.72414 1.72414 1.72414 1.72414
v5 155 1 21.2644 23.5632 24.1379 23.5632 24.1379
v6 62 2 2.87356 4.02299 4.02299 2.87356 4.02299
v7 152 4 20.6897 20.6897 20.6897 20.6897 20.6897
v8 154 1 22.4138 22.4138 22.4138 22.4138 22.4138

and I wanted to transpose them to be:-

v1 v2 ..........v8
28 77
14 .7
1.72414 7.47126
1.72414 6.89655
1.72414 6.89655
1.72414 6.89655

Please advise. Thanks
  #2 (permalink)  
Old 07-28-2008
s_becker s_becker is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 28
Taken from the GNU AWK handbook:

{
if (max_nf < NF)
max_nf = NF
max_nr = NR
for (x = 1; x <= NF; x++)
vector[x, NR] = $x
}

END {
for (x = 1; x <= max_nf; x++) {
for (y = max_nr; y >= 1; --y)
printf("%s ", vector[x, y])
printf("\n")
}
}

Copy that to a file, then invoke it like this:

awk -f scriptfile datafile | awk '{ print $8"\t" $7"\t" $6"\t" $5"\t" $4"\t" $3"\t" $2"\t" $1"\t" }'
  #3 (permalink)  
Old 07-29-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
A perl script to transpose a metrix, hope it can address your issue.


Code:
$file=shift;
open(FH,"<$file") or die "can not open file";
while(<FH>){
        $_=~ tr/\n//d;
        @arr=split(",",$_);
        $col=$#arr;
        for($i=0;$i<=$#arr;$i++){
                $index=sprintf("%s%s",$.,$i);
                $hash{$index}=$arr[$i];
        }
        $row=$.;
}
close(FH);
for($a=0;$a<=$col;$a++){
        for($b=1;$b<=$row;$b++){
                $t=sprintf("%s%s",$b,$a);
                print $hash{$t},",";
        }
        print "\n";
}

Closed Thread

Bookmarks

Tags
matrix, perl, perl shift, shift, shift perl, transpose

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0