in reply to Add Quotes to entries in a file

But, but, but, what if any of those fields is already quoted? And what if that field contains a ;?

use Text::CSV_XS "csv"; csv (in => csv (in => *DATA, sep => ";"), sep => ";", quote_always => +1, out => \my $out); print $out =~ s/^"([^"]*)"/$1/grm; __END__ ABC;123;;;;;HELLO; DEF;345;;BANANA;12DEF;44,55;4*12;;;;;;;;3;

-->

$ perl test.pl ABC;"123";"";"";"";"";"HELLO";"" DEF;"345";"";"BANANA";"12DEF";"44,55";"4*12";"";"";"";"";"";"";"";"3"; +""

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Add Quotes to entries in a file
by AnomalousMonk (Archbishop) on Jul 12, 2018 at 15:57 UTC
    ABC;"123";"";"";"";"";"HELLO";""

    niseus does not appear to want (what at first glance one would expect to be) the empty field at the end of each of the OPed example records; the semicolon seems to serve as both a field delimiter and as a record delimiter. Just to keep things simple. I'm not sure how to handle that with straight Text::CSV; my guess is the final semicolon needs separate operations to deal with it.


    Give a man a fish:  <%-{-{-{-<