File Transpose

Hi ALL
I have one input file say FILE1 which looks as below.
a=1
b=2
c=3
a=4
b=5
c=6
.
.
.

Here a,b,c...etc are variable names.

The output file(FILE2) should look like

1,2,3
4,5,6
.....
.....

If in any case any of the variable does not come in the input file then in the output file same variable should be populated as NULL.
e.g:
Input file(FILE1)
----------
a=1
b=2
c=3
a=4
c=6
.
.
.
Then output file (FILE2) should look like:
1,2,3
4,,6
.....
.....

Please help me in wrinting the above script.

Thanks in advance.

cut -d= -f2 FILE1 | paste -d ',,\n' - - -

Hi Cfajohnson,
Thanks for the reply.
Your code is working fine for the first kind of input which i have mentioned above.
But in case of second kind of input the output should look like :

1,2,3
4,,6

but its coming as :
1,2,3
4,6,

Thanks.

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

awk -F= 'END {
  for (i = 1; i <= m; i ++)
    printf "%s", (i in _ ? i : x) \
    (!(i % 3) || i == m ? RS : ",")
  }    
{ _[$2]; $2 > m && m = $2 }' infile

Independent of number of variables:

awk -F= 'function prl(){for(i in A){s=s?s","A:A;A=""}print s; s=""}
         A[$1]!=""{prl()} {A[$1]=$2} END{prl()}' infile

perl:

local $/;
my $str=<DATA>;
$str=~s/\n/ /g;
my @tmp = $str=~ /((?:a=[^abc]*)?(?:b=[^abc]*)?(?:c=[^abc]*)?)/g;
for(my $i=0;$i<=$#tmp-1;$i++){
  $tmp[$i]=~s/(?:a=([^abc]*))?(?:b=([^abc]*))?(?:c=([^abc]*))?/$1,$2,$3/;
  print $tmp[$i],"\n";
}
__DATA__
a=1
b=2
c=3
a=4
b=5
c=6
a=7
a=8
b=9
a=10
c=11
b=12
c=13
b=14
b=15
a=16
a=17
a=18
c=19

output:

1 ,2 ,3 
4 ,5 ,6 
7 ,,
8 ,9 ,
10 ,,11 
,12 ,13 
,14 ,
,15 ,
16 ,,
17 ,,
18 ,,19