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
How to transpose data elements in awk ahjiefreak Shell Programming and Scripting 2 05-13-2008 01:44 AM
How do I transpose a column of results to a row m223464 Shell Programming and Scripting 6 05-06-2008 04:33 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 06:57 AM
transpose command su_in99 UNIX for Dummies Questions & Answers 3 05-16-2007 02:10 AM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-05-2005 10:10 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-20-2005
Registered User
 

Join Date: Aug 2004
Posts: 141
Row to column transpose

Can we transpose rows to columns? Fields within row are separated by a comma.
Reply With Quote
Forum Sponsor
  #2  
Old 05-20-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by videsh77
Can we transpose rows to columns? Fields within row are separated by a comma.
yes, we can!
Reply With Quote
  #3  
Old 08-03-2005
Registered User
 

Join Date: Jul 2005
Posts: 37
but how?
Reply With Quote
  #4  
Old 08-03-2005
Registered User
 

Join Date: Jul 2005
Posts: 137
This has been discussed very recently, and an Awk solution was presented. Here's a Ruby one:
Code:
ruby -F, -lane '
BEGIN{$a=[]};$a.concat([$F]);END{$a.transpose.each{|x|puts x.join(",")}}
' infile
1,2,3,4
5,6,7,8

becomes

1,5
2,6
3,7
4,8
Reply With Quote
  #5  
Old 08-04-2005
acp's Avatar
acp acp is offline
Registered User
 

Join Date: Aug 2005
Posts: 2
shell script to tranpose rows to columns

Following Shell script works...
There should be a file namely "data" with following content
1,2,3
4,5,6
7,8,9
then after running the script the content of file will be
1,4,7
2,5,8
3,6,9

###################
num=$(awk -F"," 'NR==1 { print NF }' data)
print $num

i=1
while (( $i <= $num ))
do
newline=''
for val in $(cut -d"," -f$i data)
do
newline=$newline$val","
done
nline=`print ${newline%?}`
print $nline >> tmpdata
(( i = i + 1 ))
done
mv tmpdata data
####################
Reply With Quote
  #6  
Old 08-21-2006
Registered User
 

Join Date: Feb 2006
Posts: 52
Hello

I have one more thing to ask on same script...

what if the rows and coulmns are not equal , then the script fails...

like input file

abc,abc1,abcd3
pqrs,pqr
1,2,3,4,5,6,7,8,9,10,11

Pls suggest ....

Regards,
Aparna
Reply With Quote
  #7  
Old 08-21-2006
Playing with Ubuntu Now!
 

Join Date: Oct 2005
Location: Chennai
Posts: 364
see if it works

Put this is a file - testawk.awk
Code:
BEGIN {FS=","}

{
for (i=1;i<=NF;i++)
{
 arr[NR,i]=$i;
 if(big <= NF)
  big=NF;
 }
}
 
END {
  for(i=1;i<=big;i++)
   {
    for(j=1;j<=NR;j++)
    {
     printf("%s\t",arr[j,i]);
    }
    printf("\n");
   }
}
awk -f testawk.awk testfile
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:54 AM.


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

Content Relevant URLs by vBSEO 3.2.0