Converting rows to column

i have output of script as below

name,roll_no,01-05-12,02-05-12,03-05-12
sam,12,24,24,24
jon,145,24,24,22
van,29,24,22,24

i want to convert these into columns as output is not fixed please tell me how to convert 1st row in to 1st columns likewise,as many rows are there are to be converted into columns

thanks in advance

If you use an array, you might achieve what you need. You might need to use some indirect reference, but it would be important to know what your script is written in to advise on code.

Additionally, is your code always comma separated and would a comma ever appear as a value? Is the data set large? If so, then probably an awk rather than shell script would be better.

I hope that someone here can help. I have a few ideas, but before knowing the above, i don't want to leap into it.

Robin
liverpool/Blackburn
UK

dear rbatte1,

every row is comma separated and each rows max length is 33 char(33 values)
there will be max 15 rows have be converted into columns, but every time the length of characters in rows and number of rows is variable.
every row is comma separated.
this file contains output of several small scripts,combined into one row with comma
like

echo "$a, \c" >>sample.txt

like wise
and i didn't get the phrase "need to use some indirect reference" plz elaborate.

You statement of

echo "$a, \c" >>sample.txt

...would actually generate a comma&space separated file. I presume you mean:-

echo "$a,\c" >>sample.txt

followed by a plain echo at the end of the process.

Anyway, this indirect phrase I used.

If you are using ksh, then the arrays are limited to being one dimensional, i.e. you can only specify one ordinate. If you could specify two, then this would be easier. The logic would work that if you had arrays A and gave them ordinates x & y in a reference that identified a single value as A[x,y] then we could load the array by using a pair of loops like this (not runnable code)

x=1
y=1

until [ $x -ge 15 ]
do
   until [ $y -gt 15 ]
   do
      {read value into A[$x,$y]}
      {increment y}
   done
   {increment x}
done

Then you could write the data out again looping the other way around, such as:-

x=1
y=1

until [ $y -ge 15 ]
do
   until [ $x -gt 15 ]
   do
      {write value from A[$x,$y]}
      {increment x}
   done
   {increment y}
done

If your shell does not support two diminsional arrays (such as ksh need to check ksh93) we may have to be more inventive. The indirect variable would be a way of trying to use a variable as a variable name, so you make one of the ordinates part of the variable, so if the 2D array is A you would actually define a set of 1D arrays, A1, A2, A3, A4, ...... for as many values of x as you would have. You could then refer to $A3[5] as being array cell 3,5 as it were. The trick is how to get the array name as a variable. It could get messy.

It depends how one would write something like:-

echo "${A$x[$y]}"

.... which I'm not sure how to code up.

I'm not sure if this helps, but it may bring further suggestions - and I may learn something too!

Robin
Liverpool/Blackburn
UK

dear rbatte1

thanks for reply,but i am a very beginner in unix.I didnt get what u have exactly advised.
consider one more case
if i will break row i.e.

name,roll_no,01-05-12,02-05-12,03-05-12

and convert this into column
and same for next rows. and paste it after this first column.
will this make my task easier.