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

Hello,

I'm looking for a simple script to read a file (csv) line by line and replace double quotation marks with a single quote. There could be multiple occurrences on the same line.

Any help would be gratefully appreciated

Replies are listed 'Best First'.
Re: Substituting characters
by CountZero (Bishop) on Feb 11, 2012 at 12:38 UTC
    This does exactly what you want:
    use Modern::Perl; while (<DATA>) { tr/"/'/; print; } __DATA__ Dit, is, een, "CSV record" En, dit, "ook", ! En, hij , zei, "'Hallo, hoe gaat het?'"
    But it does not guarantee that the output is a valid CSV-file, or at least have the same number of fields in each record as before:
    Dit, is, een, 'CSV record'
    En, dit, 'ook', !
    En, hij , zei, ''Hallo, hoe gaat het?''
    
    Far better to use a module like Text::CSV to read in your CSV-file and output it again with this module, but with different delimiters (such as a single quote instead of double quotes).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      use Modern::Perl;
      Why?

      I see no reason to put mystery lines in demo scripts that have absolutely no value to the person you're talking to. You're just making it harder for them to try out your script.

        It is less typing than "use strict; use warnings; and it switches all modern features of Perl on.

        I think that is "A Good Thing™".

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Substituting characters
by Anonymous Monk on Feb 11, 2012 at 11:46 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.