Converting a text file to a csv file

I have a text file that is the output of a Netbackup report. The file it generates is just a plain text file with only white space between fields. For example:

Date Policy Type Kilobytes Retention
12/5/2005 WinNT Full 18329948 6 Months

I need to write a script that will make it look like this:

Date,Policy,Type,Kilobytes,Retention
12/5/2005,WinNT,Full,18329948,6 Months

As you can see in the first example, the number of spaces between words aren't uniform, which complicates things a bit.

If anyone has any suggestions, I would appreciate it.

This:

awk '{
     for(i=1;i<5;i++)
     { printf("%s,", $i); }
     for(i=5;i<=NF;i++)
     {printf("%s ",$i); }
     printf("\n");
     }'           filename

run against this data:

Date Policy Type Kilobytes Retention  
12/5/2005 WinNT Full 18329948 6 Months
12/5/2005 WinNT Half 100    6 Months
12/5/2005 WinNT Full 200 6 Months

Gave this output:

kcsdev:/home/jmcnama> t.awk
Date,Policy,Type,Kilobytes,Retention
12/5/2005,WinNT,Full,18329948,6 Months
12/5/2005,WinNT,Half,100,6 Months
12/5/2005,WinNT,Full,200,6 Months

first convert all retention times to use an underscore (or other symbol) instead of a space and then change the spaces in between the columns to commas

or use jim's code :slight_smile:

Jim's code worked perfectly! Thank you Jim!