convert text file to csv

hi all,
i have a select query that gives me the output in the following way...

SYSTYPE
--------------------------------------------------------------------------------
   Success   Failures      Total        RFT
---------- ---------- ---------- ----------
TYP
         1          0          1        100

TYP2
         1          0          1        100
TYP3
         1          1          2        50

::
::
::
till
TYP15
         1          1          2        50

i need to convert this into the following csv file...

SYSTYPE,Success,Failures,Total,RFT
TYP,140,8,148,900
TYP1,4,3,7,400
TYP2,26,0,26,100
TYP3,36,0,36,100
TYP4,41,0,41,100
etc......
till
TYP15,1,1,2,50

Thanks in advance..

Try this:

awk 'BEGIN{print "SYSTYPE,Success,Failures,Total,RFT"}
NR < 5 {next}
/^TYP/{if(s){print s};s=$0;next}
NF{s=s","$1","$2","$3","$4}
END{print s}' file

you can get it in CSV format from database only do following set up before running the query

set underline off
set feedback off
set colsep ","
select * from table;

If you are using Oracle then Oracle SQL developer tool, which is free, can help you a lot in this regard.