![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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" }' |
|
||||
|
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";
}
|
![]() |
| Bookmarks |
| Tags |
| matrix, perl, perl shift, shift, shift perl, transpose |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|