sql server - Removing "blank" or bad rows in a csv file using Perl. -
i working on project exports .csv files old application (a dos base application). export works, truncates column (it seems @ random) , inserts next line. it's last half of word or of sort. know, if there way use perl remove lines. use perl script remove comma delimiters , replace them pipe (|) delimiter. found using pipe makes easier insert sql use. i'm very, new perl code works replace comma's pipe. there way use , remove "bad" rows. have tried importing sql bulk insert, receive errors such "bulk load data conversion error (truncation) row 49, column 17." have looked @ export , these errors occurring there full column 17 rows, next line |cial) or |3. hope made clear. thank help.
use strict; use warnings; use text::csv_xs; $in_file = "deal_log.csv"; $out_file = "newdeal.csv"; open $fh, '<', $in_file or die "$in_file: $!"; open $out_fh, '>', $out_file or die "$out_file: $!"; $in_csv = text::csv_xs->new; $out_csv = text::csv_xs->new( { sep_char => '|', eol => "\n" } ); while( $row = $in_csv->getline( $fh ) ) { $out_csv->print( $out_fh, $row ); }
do not output line immediatelly. instead, read in next line , check number of columns. if there 17, output previous line, otherwise append line previous remembered line.
#!/usr/bin/perl use warnings; use strict; use text::csv_xs; $in_file = 'deal_log.csv'; $out_file = 'newdeal.csv'; $size = 17; open $fh, '<', $in_file or die "$in_file: $!"; open $out_fh, '>', $out_file or die "$out_file: $!"; $in_csv = 'text::csv_xs'->new; $out_csv = 'text::csv_xs'->new( { sep_char => '|', eol => "\n", } ); $previous = []; while( $row = $in_csv->getline($fh) ) { if (@$row == $size) { $out_csv->print($out_fh, $previous) if @$previous; $previous = $row; } else { $previous->[-1] .= $row->[0]; } } $out_csv->print($out_fh, $previous); # not forget print last remembered line.
Comments
Post a Comment