![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Row to column transpose
Can we transpose rows to columns? Fields within row are separated by a comma.
|
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Quote:
|
|
#3
|
|||
|
|||
|
but how?
|
|
#4
|
|||
|
|||
|
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
5,6,7,8 becomes 1,5 2,6 3,7 4,8 |
|
#5
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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");
}
}
|
|||
| Google The UNIX and Linux Forums |