Converting rows to columns in csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting rows to columns in csv file
# 8  
Old 12-28-2012
My solution (I need to practice awk Smilie)

file
Code:
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6
p1,p2,p3,p4,p5,p6
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6

script.awk
Code:
BEGIN {
  FS=","
}

{
  for (i = 1; i <= NF; i++)
      arr[i,NR]=$i
}

END {
  for (x = 1; x <= NR; x++)
  {
    for (y = 1; y <= NF; y++)
      {
        printf("%s", arr[x,y])
        if(y < NF)
          printf("%s", ",")
      }
    print ""
  }
} # END

Execution :
Code:
#awk -f script.awk < file

result
Code:
c1,r1,p1,w1,y1,z1
c2,r2,p2,w2,y2,z2
C3,r3,p3,w3,y3,z3
c4,r4,p4,w4,y4,z4
c5,r5,p5,w5,y5,z5
c6,r6,p6,w6,y6,z6

It should work for a more than 6x6 matrix

---------- Post updated 28th Dec 2012 at 10:38 AM ---------- Previous update was 27th Dec 2012 at 03:35 PM ----------

A great majority of the scripts at my new office are written in KSh, but some Perl scripts remain in some servers.
I need to practice Perl too, so hereafter the Perl version of the previous awk solution :
Code:
#!/usr/bin/perl -w
use strict;
 
my $cur_dir = $ENV{PWD};
my $filename = $cur_dir."/file";
 
open(FILE,"<$filename") or die"open: $!";
my ($record,@fields,%col);
while( defined( $record = <FILE> ) ) {
  my $col_counter=1;
  chomp $record;
  @fields=split(/,/,$record);
  foreach (@fields) {
    $col{$col_counter} .= "$_,";
    $col_counter++;
  }
}
 
close(FILE);
 
for ( my $i=1; $i<=keys(%col); $i++ ) {
  my $line=$col{$i};
  chop $line;
  print "$line\n";
}

If Awk of Perl gurus have comments about my code, they are welcome Smilie.

I'm also wondering what is the best choice for my future new scripts : KSh+awk or Perl ?

Thank You
# 9  
Old 12-28-2012
Need some corrections to your script..

Please check..

Your script may work on 6*6 or 9*9 matrix. But try on 6*9 or sth like this. Smilie

Code:
BEGIN {
  FS=","
}

{
  for (i = 1; i <= NF; i++)
      arr[i,NR]=$i
}

END {
  for (x = 1; x <= NF; x++)
  {
    for (y = 1; y <= NR; y++)
      {
        printf("%s", arr[x,y])
        if(y < NR)
          printf("%s", ",")
      }
    print ""
  }
}


Last edited by pamu; 12-28-2012 at 05:57 AM..
# 10  
Old 12-28-2012
Hi Pamu,

my awk script is working well.
Can you explain your modifications ?

Thank You
# 11  
Old 12-28-2012
Quote:
Originally Posted by Fundix
Hi Pamu,

my awk script is working well.
Can you explain your modifications ?

Thank You
Please check..

Code:
$ cat file
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6
p1,p2,p3,p4,p5,p6
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6
p1,p2,p3,p4,p5,p6
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6

$ awk 'BEGIN {
  FS=","
}

{
  for (i = 1; i <= NF; i++)
      arr[i,NR]=$i
}

END {
  for (x = 1; x <= NR; x++)
  {
    for (y = 1; y <= NF; y++)
      {
        printf("%s", arr[x,y])
        if(y < NF)
          printf("%s", ",")
      }
    print ""
  }
}' file
c1,r1,p1,w1,y1,z1
c2,r2,p2,w2,y2,z2
C3,r3,p3,w3,y3,z3
c4,r4,p4,w4,y4,z4
c5,r5,p5,w5,y5,z5
c6,r6,p6,w6,y6,z6
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,

Code:
$ cat file
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6
p1,p2,p3,p4,p5,p6
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6
p1,p2,p3,p4,p5,p6
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6

$ awk 'BEGIN {
  FS=","
}

{
  for (i = 1; i <= NF; i++)
      arr[i,NR]=$i
}

END {
  for (x = 1; x <= NF; x++)
  {
    for (y = 1; y <= NR; y++)
      {
        printf("%s", arr[x,y])
        if(y < NR)
          printf("%s", ",")
      }
    print ""
  }
}' file
c1,r1,p1,w1,y1,z1,c1,r1,p1,w1,y1,z1
c2,r2,p2,w2,y2,z2,c2,r2,p2,w2,y2,z2
C3,r3,p3,w3,y3,z3,C3,r3,p3,w3,y3,z3
c4,r4,p4,w4,y4,z4,c4,r4,p4,w4,y4,z4
c5,r5,p5,w5,y5,z5,c5,r5,p5,w5,y5,z5
c6,r6,p6,w6,y6,z6,c6,r6,p6,w6,y6,z6

This User Gave Thanks to pamu For This Post:
# 12  
Old 12-28-2012
Merci Pamu Smilie
# 13  
Old 12-28-2012
considering number of columns are not the same.

With some assumptions and assuming required output is as below(with comma separated for null values)Smilie

Code:
$ cat file
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6
p1,p2,p3,p4,p5,p6,p7
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6
c1,c2,C3,c4,c5,c6
r1,r2,r3,r4,r5,r6,r7,r8
p1,p2,p3,p4,p5,p6
w1,w2,w3,w4,w5,w6
y1,y2,y3,y4,y5,y6
z1,z2,z3,z4,z5,z6

$ awk -F, '{for(i=1;i<=NF;i++){A[NR,i]=$i};if(NF>n){n=NF}}
END{for(i=1;i<=n;i++){
s=A[1,i]?A[1,i]:"  ";
for(j=2;j<=NR;j++){
p=A[j,i]?A[j,i]:"  "
s=s","p}
print s}}' file

c1,r1,p1,w1,y1,z1,c1,r1,p1,w1,y1,z1
c2,r2,p2,w2,y2,z2,c2,r2,p2,w2,y2,z2
C3,r3,p3,w3,y3,z3,C3,r3,p3,w3,y3,z3
c4,r4,p4,w4,y4,z4,c4,r4,p4,w4,y4,z4
c5,r5,p5,w5,y5,z5,c5,r5,p5,w5,y5,z5
c6,r6,p6,w6,y6,z6,c6,r6,p6,w6,y6,z6
  ,  ,p7,  ,  ,  ,  ,r7,  ,  ,  ,
  ,  ,  ,  ,  ,  ,  ,r8,  ,  ,  ,

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting columns of text to rows, with blank lines

I've spent the past hour trying different things and googling for this solution and cannot find the answer. Found variations of this, but not this exact thing. I have the following text, which is the output from our mainframe. Each field is on a separate line, with a blank line between each... (7 Replies)
Discussion started by: lupin..the..3rd
7 Replies

2. Shell Programming and Scripting

Converting rows into columns

hi all I need a help ..I have a script that takes has command and its output is like below.. a b a v v a c I am assigning the above outputs to a variable .. <variable name> = <command output> problem here is when I echo the variable ..it gives me output like " a b... (3 Replies)
Discussion started by: shankarb
3 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. UNIX for Dummies Questions & Answers

Converting Columns To Rows Sort Of

Hi I'm a UNIX awk and sed novice at best. I'm trying to creat a .csv file so it can be graphed in Excel. Tried various xargs, awk, sed and paste but just can't seem to get the data to line up. Not sure if this is beyond for a question in these forums. Any help would greatly be appreciated. Have... (4 Replies)
Discussion started by: jimmyf
4 Replies

5. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Converting rows to columns using shell script

I have a script which converts rows to columns. file_name=$1 mailid=$2 #CREATE BACKUP OF ORIGINAL FILE #cp ${file_name}.xlsx ${file_name}_temp.xlsx #tr '\t' '|' < ${file_name}_temp.xlsx > ${file_name}_temp.csv #rm ${file_name}_temp.xlsx pivot_row=`head -1 ${file_name}` sed 1d... (3 Replies)
Discussion started by: andy538
3 Replies

7. Shell Programming and Scripting

converting rows into columns

Hi, I am trying to fetch some values from db and spooling the output to a file. when i query the db for the values, i get the values in following format. PC_1 wf_test1 Test PC_2 wf_test2 Test PC_3 wf_test3 Test But my spool file was created in following format. PC_1 wf_test1 Test... (20 Replies)
Discussion started by: svajhala
20 Replies

8. UNIX for Dummies Questions & Answers

Converting columns into rows

Is there anyway to convert columns into raws using awk? (or any other command line):eek::eek::eek::eek::eek::eek::eek::eek::eek: (1 Reply)
Discussion started by: cosmologist
1 Replies

9. Shell Programming and Scripting

how to converting rows to columns, bash

I have in file these words: @fraza1 = rw @fraza2 = r @fraza3 = r @fraza4 = r @fraza5 = r @fraza1 = r @fraza6 = r @fraza7 = r @fraza2 = r @fraza8 = r @fraza9 = r ... I would like so that: ,rw,@fraza1 ,r,@fraza2 (2 Replies)
Discussion started by: patrykxes
2 Replies

10. Shell Programming and Scripting

deleting rows & columns form a csv file

Hi , I want to delete some rows & columns from file. can someone please help me on this? Regards. (2 Replies)
Discussion started by: code19
2 Replies
Login or Register to Ask a Question