I have an input csv file, comma delimited. The trouble is there are some fields which look like this;
field1,another_one,"hello, there",2,3,4
This obviously will cause an issue as most import routines will see this as two separate fields. Ideally, I'd like to parse the file, change the commas to tabs, and get rid of double-quotes.
This Perl sample shows how to open a text file, parse it, write it out to a new file, as well as how to loop through an array of arrays and how to use the Perl Text::ParseWords module.
#!/usr/bin/perl
#
# Perl script to change commas to tabs in comma-delimited CSV
# This also avoids issue of commas embedded in fields
# (in case you think a one-line sed would have achieved this!)
#
print "Converting from comma to tab delimited \n"; # Print a message
use Text::ParseWords;
local $/ = "\n"; # tell chomp to look for LF (for CRLF change this to \r\n)
# delete existing output file, open input and output files
unlink("sample_output_tabs.csv");
open (INFILE, './sample_output.csv');
open (OUTFILE, '>>sample_output_tabs.csv');
while () {
chomp;
$_ =~ s/\'//g; # remove single quotes
@lists = &nested_quotewords(',', 0, $_); # parse each line
push @arr,@{$_} foreach(@lists);
for($i=0;$i<=$#arr-1;$i++){
print OUTFILE $arr[$i];
if ($i != 14) {print OUTFILE "\t";} # put tab on end if not the last field
}
@arr = ();
print OUTFILE "\n"; # line feed on end of each line
}
close (INFILE);
close (OUTFILE);