text to csv conversion

June 3, 2011 at 02:19:35
Specs: Linux i686, 256
Thank u every body ......just need a help so that a text file needs to be converted into CSV.............

my log file is as follows

Host scsi3: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 09:01:21 GMT 2011

Host scsi3: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 11:22:14 GMT 2011

Host scsi4: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 11:29:44 GMT 2011

Thank u every body ......just need a help so that a text file needs to be converted into CSV.............

my log file is as follows

Host scsi3: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 09:01:21 GMT 2011

Host scsi3: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 11:22:14 GMT 2011

Host scsi4: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 11:29:44 GMT 2011


Now how can i convert this to csv file so that i can export it into some database

for example:-

scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks
usb-storage, Maxtor, OneTouchIII, 044303E5, Transparent SCSI, Bulk, Tue May 31 09:01:21 GMT 2011
usb-storage, Maxtor, OneTouchIII, 044303E5, Transparent SCSI, Bulk, Tue May 31 11:22:14 GMT 2011
usb-storage, Maxtor, OneTouchIII, 044303E5, Transparent SCSI, Bulk, Tue May 31 11:29:44 GMT 2011

after some googling i got a clue about getline command with awk....... but dont know how to use;............

any suggestions in this regards greatly appreciated
regards



See More: text to csv conversion

Report •

#1
June 3, 2011 at 10:52:38
This awk script was created on Solaris 9 Unix, but should be portable to windows. The key is to delimit the fields with a colon and print only field 2 except for the date string and skip the Quirks field.

If you include the Quirks field, then your output data would have an extra comma after the transport column since your data field contains no Quirks:

#!/bin/ksh

awk ' BEGIN { FS=":"
print "scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks"
}
{
if($1 ~ /^Host/ || $1 == "Vendor" || $1 == "Product" || $1 == "Serial Number" || $1 == "Protocol" || $1
 == "Transport" )
   {
   printf("%s,", $2)
   continue
   }

if($1 == "Quirks")
   continue # do nothing for Quirks

# anything left is a blank line or date string print with a carriage return
if(NF > 0)
   printf("%s\n", $0)

} ' datafile.txt


Report •
Related Solutions


Ask Question