roho has asked for the wisdom of the Perl Monks concerning the following question:
In the code below, I am replacing the comma with a dash in the quoted string in each input line. The code works, but I'm wondering if there is a way to do this in one statement. I tried a few variations but the stumbling block was always that the "capture" variable $1 is read-only.
BTW, before someone mentions using Text::CSV_XS, I have a version that uses the module but it takes over 4 times longer to process. The real files I will be processing will be huge and I need all the speed I can squeeze out of this.
FYI: The reason I am doing this is because SQL Server BULK INSERT does not respect quoted values. It sees the comma in the quoted string and treats it as a field terminator. :(
#!/usr/bin/perl use strict; use warnings; while (<DATA>) { chomp; m/"([^"]+)"/; # Extract quoted value. my $val = $1; # Save value in variable. $val =~ s/,/-/; # Change comma to dash. s/"[^"]+"/$val/; # Replace quoted value. print $_, "\n"; } __DATA__ 65722417,"1193,1",7980,1133566,4169735,035,FEDERAL UNIVERSAL SERVICE F +UND,0.12998 65722417,"1193,1",1012,1132900,4150053,C2,Carrier Cost Recovery Fee,0. +0273
"Its not how hard you work, its how much you get done."
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Modifying CSV File
by AnomalousMonk (Archbishop) on Jun 16, 2015 at 16:16 UTC | |
by roho (Bishop) on Jun 16, 2015 at 16:49 UTC | |
Re: Modifying CSV File
by Tux (Canon) on Jun 17, 2015 at 10:09 UTC | |
by roho (Bishop) on Jun 17, 2015 at 19:53 UTC | |
by Tux (Canon) on Jun 18, 2015 at 06:46 UTC | |
by roho (Bishop) on Jun 18, 2015 at 09:55 UTC | |
by Tux (Canon) on Jun 18, 2015 at 12:41 UTC | |
| |
Re: Modifying CSV File
by Anonymous Monk on Jun 16, 2015 at 22:58 UTC |
Back to
Seekers of Perl Wisdom