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
How do I transpose a column of results to a row m223464 Shell Programming and Scripting 6 05-06-2008 08: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 09:57 AM
transpose command su_in99 UNIX for Dummies Questions & Answers 3 05-16-2007 06:10 AM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-06-2005 02:10 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 05-20-2005
videsh77 videsh77 is offline
Registered User
  
 

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

Can we transpose rows to columns? Fields within row are separated by a comma.
  #2 (permalink)  
Old 05-20-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by videsh77
Can we transpose rows to columns? Fields within row are separated by a comma.
yes, we can!
  #3 (permalink)  
Old 08-03-2005
mskcc mskcc is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 37
but how?
  #4 (permalink)  
Old 08-04-2005
futurelet futurelet is offline
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
  #5 (permalink)  
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
####################
  #6 (permalink)  
Old 08-21-2006
er_aparna er_aparna is offline
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
  #7 (permalink)  
Old 08-21-2006
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
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
Closed Thread

Bookmarks

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 11:41 PM.


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