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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
just want certail elements frenchface Shell Programming and Scripting 1 05-10-2008 06:38 PM
How do I transpose a column of results to a row m223464 Shell Programming and Scripting 6 05-06-2008 04:33 AM
Map - printing all elements - why? dhanamurthy High Level Programming 0 04-14-2008 10:19 AM
Row to column transpose videsh77 Shell Programming and Scripting 10 06-16-2007 09:54 AM
transpose command su_in99 UNIX for Dummies Questions & Answers 3 05-16-2007 02:10 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: Nov 2007
Posts: 127
Stumble this Post!
How to transpose data elements in awk

Hi,

I have an input data file :-
Test4599,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,2,R ain
Test90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,N ot Rain
etc....

I wanted to transpose these data to:-

Test4599 Test90
0 0
0 0
.. ...
In other words, I wanted to transpose elements from vertical to horizontal.
Any idea on how to do that using Awk?

Please advise.

Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-13-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
The "similar threads" box near the bottom of the page looks vaguely promising.
Reply With Quote
  #3 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 5
Stumble this Post!
awk -F, 'BEGIN {max_i=0} { \
for(i=1;i<=NF;i++) {text_arr[NR SUBSEP i]=$NF} \
if ( NF > max_i ) {max_i = NF}} \
END { for(i=1;i<=max_i;i++) { for(j=1;j<=NR;j++)\
printf text_arr[j SUBSEP i];print}}}' file.txt

I did not tested it, but should be very close to the final solution.
Luck
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0