Convert comma text file to Column in html format

I am trying to generate a report with below file :

File1 :

EQADM,edrtere9-phys,8122caef0,gpatmon,/bin/ksh,nuten Erick
EQADM,edrtere11-phys,8227caef0,gpatmon,/bin/ksh,nuten Erick
EQADM,edrtere3-phys,822caef0,gpatmon,/bin/ksh,nuten Erick

can you help me convert it to html and add columns in places where there is comma ?

Thanks

This could be achieved with a one liner awk script ... Using your example of File1 ...

% awk -F, 'BEGIN {print "<table>"} ; {  print "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td><td>" $4 "</td><td>" $5 "</td><td>" $6 "</td></tr>"} ; END { print "</table>"}' File1

would result in the following output:

<table>
<tr><td>EQADM</td><td>edrtere9-phys</td><td>8122caef0</td><td>gpatmon</td><td>/bin/ksh</td><td>nuten Erick</td></tr>
<tr><td>EQADM</td><td>edrtere11-phys</td><td>8227caef0</td><td>gpatmon</td><td>/bin/ksh</td><td>nuten Erick</td></tr>
<tr><td>EQADM</td><td>edrtere3-phys</td><td>822caef0</td><td>gpatmon</td><td>/bin/ksh</td><td>nuten Erick </td></tr>
</table>
> inv="EQADM,edrtere9-phys,8122caef0,gpatmon,/bin/ksh,nuten Erick"
> echo $inv | awk -F"," '{printf("%15s%15s%15s%15s%15s%15s\n",$1,$2,$3,$4,$5,$6)}'
          EQADM  edrtere9-phys      8122caef0        gpatmon       /bin/ksh    nuten Erick

or

> echo $inv | awk -F"," '{printf("%-15s%-15s%-15s%-15s%-15s%-15s\n",$1,$2,$3,$4,$5,$6)}'
EQADM          edrtere9-phys  8122caef0      gpatmon        /bin/ksh       nuten Erick    
echo "EQADM,edrtere9-phys,8122caef0,gpatmon,/bin/ksh,nuten Erick " | sed -e 's/^/<tr><td>/' -e 's/,/<\/td><td>/g' -e 's/$/<\/td><\/tr>/'

Try...

awk 'BEGIN{
        FS=","
        print "<HTML><BODY><TABLE>"
     }
     {
        printf "<TR>"
        for(i=1;i<=NF;i++)
          printf "<TD>%s</TD>", $i
        print "</TR>"
     }
     END{
        print "</TABLE></BODY></HTML>"
     }
     ' file1.csv > file2.html

Hi Ygor,
If the number of columns are more than 100, the above convert to html is not working.
Any solution if the columns are more than 100?

Thanks.

Use nawk instead of awk to support beyond 100 columns.

Srini

Hi Srinivas,
Thanks for the response.
One more issue now.
All the leading zeros are truncated when converted to xls.
How do I preserve them?

Thanks

Hello there,

You can actually preserve them by maintaining the column as "text" formatted.

rgds,

Srini

Thanks all,

I tried this and it worked well

Any ideas how do i send it as HTML format in mail

I tried adding Content-type: text/html; on top <HTML> tag it did not work for me.

While using mailx command

cat index.html |mailx -s "test" sriram1 at my mailid.com
it send the mail with TAgs !