Text::CSV_XS Trying to find empty field : awk
This is a discussion on Text::CSV_XS Trying to find empty field within the awk forums in Programming Languages category; Hello I was wondering if any one could answer his quetion for me. I have a comma seperated file in which looks like the one below. "LIBjj91474","","FMOM95:088- VT Call Forwarding Unconditional (CFU)","N","1","1","","Consumer_Experience", etc. What I am trying to do is parse the CSV file for a particular column number say column 11. when the column is empty (which is when its "", I want to add data by writing to it spreadsheet. I want to do this for each row in the column. that is empty. I already have the ability to write to the spreadsheet. This is another requirement. ...
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| I was wondering if any one could answer his quetion for me. I have a comma seperated file in which looks like the one below. "LIBjj91474","","FMOM95:088- VT Call Forwarding Unconditional (CFU)","N","1","1","","Consumer_Experience", etc. What I am trying to do is parse the CSV file for a particular column number say column 11. when the column is empty (which is when its "", I want to add data by writing to it spreadsheet. I want to do this for each row in the column. that is empty. I already have the ability to write to the spreadsheet. This is another requirement. my $filename ='textfile.txt'; open(FILE,">$filename") || die("Cannot Open File $filename : $!" ); print FILE $query_result->content; print "File open "; close (FILE); # Open the Comma Separated Variable file open (CSVFILE, $filename) or die "$filename: $!"; # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new('Filename.xls'); my $worksheet = $workbook->add_worksheet(); my $format3 = $workbook->add_format(); $format3->set_text_wrap(); $format3->set_border(); $format3->set_bottom(); $format3->set_top(); $format3->set_left(); $format3->set_right(); # Create a new CSV parsing object my $csv = Text::CSV_XS->new; # Row and column are zero indexed my $row = 0; my $total; my $count; while (<CSVFILE>) { if ($csv->parse($_)) { my @Fld = $csv->fields; my $col = 0; foreach my $token (@Fld) { $worksheet->write($row, $col, $token, $format3); $col++; } $row++; if ($row > 1){ $count = $count + 1; $total = $count; } } else { my $err = $csv->error_input; print "Text::CSV_XS parse() failed on argument: ", $err, "\n"; } I have tried other ways of checking for empty contents but it just is not giving me what I need. I'm thinking since parsing is taking place why not parse for empty field here and write to spread sheet at same time rather then do it later. Just need some help in checking for empty comma delimited field. Thanks, Pam |
|
#2
| |||
| |||
| In article <1159825533.109768.10730@k70g2000cwa.googlegroups. com>, Pam <pamelapdh@aol.com> wrote: >Hello Hi. >open(FILE,">$filename") || die("Cannot Open File $filename : $!" ); >print FILE $query_result->content; >print "File open "; Um, what language is that? I'm afraid I don't quite see it. And, er, what newsgroup is this? |
|
#3
| |||
| |||
| Pam wrote: > Hello > > > I was wondering if any one could answer his quetion for me. > I have a comma seperated file in which looks like the one below. > > > "LIBjj91474","","FMOM95:088- VT Call Forwarding Unconditional > (CFU)","N","1","1","","Consumer_Experience", etc. > > > What I am trying to do is parse the CSV file for a particular column > number say column 11. Look: $ cat file "LIBjj91474","","FMOM95:088- VT Call Forwarding Unconditional (CFU)","N","1","1","","Consumer_Experience" $ awk -F'","' '{gsub(/(^"|"$)/,""); for (i=1;i<=NF;i++) printf "field %d = \"%s\"\n",i,$i}' file field 1 = "LIBjj91474" field 2 = "" field 3 = "FMOM95:088- VT Call Forwarding Unconditional (CFU)" field 4 = "N" field 5 = "1" field 6 = "1" field 7 = "" field 8 = "Consumer_Experience" so, to select field 3, say (since there is no 11th field), would just be: $ awk -F'","' '{gsub(/(^"|"$)/,""); print $3}' file FMOM95:088- VT Call Forwarding Unconditional (CFU) > when the column is empty (which is when its "", I want to > add data by writing to it spreadsheet. I want to do this for each row > in the column. > that is empty. I don't know what you mean by "add data by writing to it spreadsheet", but if you just want to add some text saying some string like "empty" then all you need is: $ awk ' BEGIN{FS=OFS="\",\""} { gsub(/(^"|"$)/,"") for (i=1;i<=NF;i++) $i = ($i == "" ? "empty" : $i) printf "\"%s\"\n",$0 }' file "LIBjj91474","empty","FMOM95:088- VT Call Forwarding Unconditional (CFU)","N","1","1","empty","Consumer_Experience" Regards, Ed. |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ANNOUNCE: Text::CSV_XS 0.32 | usenet | Perl | 7 | 11-12-2007 10:20 PM |
| ANNOUNCE: Text::CSV_XS 0.32 | usenet | Perl | 0 | 10-24-2007 06:45 AM |
| ANNOUNCE: Text::CSV_XS 0.26 | usenet | Perl | 0 | 05-15-2007 06:30 AM |
| ANNOUNCE Text::CSV_XS 0.26 | usenet | Perl | 0 | 05-15-2007 06:29 AM |
| [ANNOUNCE] Text-CSV_XS 0.25 | usenet | Perl | 0 | 05-07-2007 10:22 AM |
All times are GMT -5. The time now is 04:27 AM.




