Generate .csv file a text file

Hi guys,

I have a text file that states:

cat prod.hosts
11.29.130.14  host1        host1.test.com        web17
11.29.130.15  host2        host2.test.com         web18
11.29.130.16  host3        host3.test.com         web19

I want a .csv file that states:

11.29.130.14,host1,host1.test.com,web17
11.29.130.15,host2,host2.test.com,web18
11.29.130.16,host3,host3.test.com,web19

How can this be achieved ?

I am trying to use a for loop like:

for i in `cat prod.hosts`
do 
         IP=`cat prod.hosts | awk '{print $1}'
         Hostname=`cat prod.hosts | awk '{print $2}'
         FQDN=`cat prod.hosts | awk '{print $3}'
         TYPE=`cat prod.hosts | awk '{print $4}'

         echo "$IP,$HOSTNAME,$FQDN,$TYPE" >> FILE.csv
done

Rather than using cat and awk four times per input line, I would just use one call to awk for each input file:

awk '$1=$1' OFS=, prod.hosts > FILE.csv

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk .

1 Like

Thanks ! Worked like a charm !