CSV - How to quote the nth column?

CSV - How to quote the nth column?

1581
Power UserPower User
1581

    Mar 04, 2021#1

    I have a CSV (tab-based or with fixed width ..) like this:

    Code: Select all

    ID         Name        Value A     Value B
    1          John Doe    a b         1 2 3
    1234568    J. F. Ken               some thing
    To copy and paste it to another software, I need to quote some columns, here for example the 2nd:

    Code: Select all

    ID         'Name'        Value A     Value B
    1          'John Doe'    a b         1 2 3
    1234568    'J. F. Ken'               some thing
    Any (simple) ideas?
    I can help myself with other features, but if this "feature" would already exist it would be fine ..
    UE 26.20.0.74 German / Win 10 x 64 Pro

    6,688587
    Grand MasterGrand Master
    6,688587

      Mar 04, 2021#2

      A Perl regular expression replace all with search string ^[^\t]*?\t\K((?![\t'])[^\t]+) and replace string '\1' could be used on file being a TSV file (tab-separated values file). No field value for second data column and field values in second data column starting with ' are ignored by the Perl regular expression search string.

      For a fixed width file with spaces can be used for this example the Perl regular expression search string ^.{11}(?![ '])\K(.+?(?=  )) with the replacing string '\1' as above. Two spaces define the end of the string to enclose in single quotes.

      It do not really understand why using ' and not " because of ' has no special meaning in a comma-separated values file in comparison to ".
      Best regards from an UC/UE/UES for Windows user from Austria

      1581
      Power UserPower User
      1581

        Mar 04, 2021#3

        Thanks, Mofi, I will try it.

        The reason of using single quotes ' is that finally I paste the code to a SQL Statement. And there double quote " is not accepted.

          Mar 12, 2021#4

          Mofi wrote:
          Mar 04, 2021
          .... ^[^\t]*?\t\K((?![\t'])[^\t]+) ...
          Every time when I think that I understand the first percent of RegEX I have to see something like this - then I need a double coffee and sit down in the corner of my room and cry ... 😢

          Thanks, and greetings to Vienna.

          PS: For all users who are also filling their coffee cups with tears (while crying): this great page translate the code to human words https://regex101.com/
          UE 26.20.0.74 German / Win 10 x 64 Pro

          19176
          MasterMaster
          19176

            Mar 12, 2021#5

            Peter, don't give up. Unlike understanding to women, regexes are actually really simple :)

            BTW I would use
            ^[^\t]*+\t\K
            instead of
            ^[^\t]*?\t\K

            Long live the possessive quantifiers!