in reply to One Liner to double quote every field in a delimited file

perl -pe"s[,][\",\"]g;s[^][\"];s[$][\"];" infile > outfile

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: One Liner to double quote every field in a delimited fle (Updated!)
  • Download Code

Replies are listed 'Best First'.
Re^2: One Liner to double quote every field in a delimited file
by Skeeve (Parson) on Feb 01, 2014 at 16:42 UTC

    -- because it does not work:

    $ perl ./browseruk.pl "Joe","43,anytown,anystate,zipcode"

    You forgot a "g" modifier.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      You forgot a "g" modifier.

      Indeed I did. Thanks. Now corrected.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: One Liner to double quote every field in a delimited fle (Updated!)
by Jim (Curate) on Feb 01, 2014 at 22:04 UTC

    It's a common mistake to forget to escape literal occurrences of the same character used to "quote" fields.

    Windowsish

    perl -pe "s{(?=\")}{\"}g; s{,}{\",\"}g; s{^}{\"}; s{$}{\"};" infile.csv > outfile.csv

    Unixish

    perl -pe 's{(?=")}{"}g; s{,}{","}g; s{^}{"}; s{$}{"};' infile > outfile
      It's a common mistake to forget to escape literal occurrences of the same character used to "quote" fields.

      I didn't forget.

      I just didn't make the mistake of over-engineering a one-liner to cater for unspecified requirements.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: One Liner to double quote every field in a delimited fle (Updated!)
by BOK_in_NEPA (Initiate) on Feb 03, 2014 at 13:08 UTC
    Awesome!! Thanks! I wasn't sure how to combine multiple statements into one command..
      sub one_command { ... multiple statements here ... }