
Hannibal_32
New User
Feb 22, 2012, 9:11 AM
Post #1 of 5
(1134 views)
|
|
Using TEXT:CSV_XS to push headers
|
Can't Post
|
|
I have a .csv file that is formatted like this:
# SystemId: G9 Gateway.125 # STARTTIMESTAMP , STOPTIMESTAMP , SYSGRPNUM , GROUPTYPE , INCOMINGATTEMPTS , OUTGOINGATTEMPTS , SIGFAILURES , TOTALUSAGESEC , OVERFLOWS , OVERFLOWSEC , MINRESOURCEAVAIL , MINRESOURCEAVAILVALID 2012-02-16 21:30:00,2012-02-16 22:00:00,1,4,0,0,0,0,0,0,0,1 What I am trying to do is copy the headers out into another file where I will push them into an SQL database. I would like them pushed into the new file like this:
header1,header2,header3,header4,header5 This is what I have so far, but am stumped on what to do next:
#!/usr/local/bin/perl use Text::CSV_XS; use strict; use warnings; print "-------------------------------\n"; print "-------------------------------\n"; print ".\n"; print "..\n"; print "Please choose which file you would like parse:\n"; $choice=<STDIN>; chomp $choice; print "-------------------------------\n"; print "-------------------------------\n"; print "What would you like the table to be titled?\n\n"; $tablename=<STDIN>; our $data= "/opt/home/Xenesis/PM/files/$choice"; open(POST, "</opt/home/Xenesis/PM/files/$choice") or die "Could not open '$data'\n"; my $csv = Text::CSV_XS->new({ sep_char => ',' }); while (my $line = <POST>) { if ($csv->parse($line)) { my @fields = $csv->fields(); print "$line\n"; } else { warn "Line unable to be parsed: $line\n"; } } I am using the TEXT::CSV_XS module as you can see, but I am lost on how to use it correctly. Any help is appreciated. Thanks, H
|