beerman has asked for the wisdom of the Perl Monks concerning the following question:

I need to modify a field in a CSV file. I can use text::csv_xs to parse the file and change the field but when I write the fields back to the output file, I can't get the double quotes and comma (delimiters) to print. In other words, the only change to the input CSV file content should be the field my perl script modifies.

For example, with data such as

"What, a nice day",This is a field,"Here is an,and day"

The above has 3 fields. Let's say that the perl script modifies the 2nd entry to change 'field' to 'fence'. My perl script does this with no problems but it prints the output without the quotes or commas with the exception of commas inside a field.

Here is my output

What, a nice day This is a fence Here is an,and day

but I wanted the output to be the same as the input with only the field change that my script made. SO the desired out is

"What, a nice day",This is a fence,"Here is an,and day"

I would greatly appreciate some help on how to solve this problem

Replies are listed 'Best First'.
Re: how to modify a field in a CSV file
by blue_cowdawg (Monsignor) on Sep 02, 2011 at 17:08 UTC
        I need to modify a field in a CSV file.

    #!/usr/bin/perl -w # Caveat: untested code. Don't do anything foolish with it. use strict; use Tie::File; tie my @ry,"Tie::File", "MyFile.csv" or die "MyFile.csv:$!"; my $i = 10; # or whatever my @j = split(",",$ry[$i]); $j[1] = "This is a fence"; $ry[$i] = join(",",@j); # #############################

    Obviously you'd have to customize the above to suite your needs.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Peter, Thanks for the suggestion. My data is quite flexible and I only provided an example for illustrative purposes. My goal is to modify a field in a CSV file. The input to my script is the field (index) to modify and the change to the field. As previously mentioned, it is easy to parse a CSV file using text:csv_xs and get the field that the user wants to change and then make the change. My only problem is that I've not figured out how to write the new file with just the change to the field. In other words, I'm trying to retain the double quotes and the comma delimiters that were part of the input file. I'd like to think that I can do this with text::csv_xs but I've not figured out how to do it yet so that is the reason for this post
        I'm trying to retain the double quotes and the comma delimiters that were part of the input file. I'd like to think that I can do this with text::csv_xs

        Sure, see combine().

        my $csv = Text::CSV->new ( { quote_space => 0 } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); my @columns = ("What, a nice day", "This is a fence", "Here is an,and +day"); print $csv->string() if $csv->combine(@columns);

        generates the following line:

        "What, a nice day",This is a fence,"Here is an,and day"