Replace rows to column

I have a csv file and i want to convert its rows into columns

sample file like this

Row1,1,2,3,......,n
row2,4,5,6,.......,n
.
.
.
.
rown,7,8,9,........,n

i want it like this

row1,row2,....,rown
1,4,.............,7
2,5,.............,8
3,6,.............,9

is it possible to do so.

Almost certainly, and probably done before in these forums. Any attempts from your side? And, please use code tags, not quote tags.

Dear RudiC,

plz let me the logic how to do it, because the no of rows and columns are variable (may change every time).

Hi Sagar,

Here is an awk approach. Lets say we have an example input file as follows.

cat test15
Row1    Row2    Row3    Row4    Row5
435 BL_lmapm03 rrr RDF1+TDEV 0cef 45 mask1
435 BL_lmapm03 rrr TDEV 080a 50 mask2
435 BL_lmapm02 fff RDF1+TDEV 0ceg 45 mask4
435 BL_lmapm02 fff TDEV 080b 60 mask6
435 BL_lmapm06 hhh TDEV 080f 60 mask9

Then code will be as follows.

awk '{if(NR==1) {print $0} else {for(i=1;i<=NF;i++){a_i=a_i?a_i OFS $i:$i}}} END{for(i=1;i<=NF;i++){print a_i}}' test15

Output will be as follows.

Row1    Row2    Row3    Row4    Row5
435 435 435 435 435
BL_lmapm03 BL_lmapm03 BL_lmapm02 BL_lmapm02 BL_lmapm06
rrr rrr fff fff hhh
RDF1+TDEV TDEV RDF1+TDEV TDEV TDEV
0cef 080a 0ceg 080b 080f
45 50 45 60 60
mask1 mask2 mask4 mask6 mask9

Hope this will be helpful for you.

Thanks,
R. Singh

1 Like

Try Converting rows to columns in csv file Post: 302748923