regex to remove comma between double quotes notepad++

前端 未结 4 2059
刺人心
刺人心 2020-12-05 05:50

I am trying to remove commas inside double quotes from a csv file in notepad++, this is what I have:

1070,17,2,GN3-670,\"COLLAR B, M STAY\",\"2,606.45\"
         


        
4条回答
  •  余生分开走
    2020-12-05 06:19

    For a line with multiple instances of "comma within double quotes", I can think of the following perl script - you need to have a header line without this kind of instance so that you know how many comma-separated fields there should be.

    #! /usr/bin/perl -w
    
    use strict;
    
    my $n_fields = "";
    while (<>) {
        s/\s+$//;
        if (/^\#/) { # header line
            my @t = split(/,/);
            $n_fields = scalar(@t); # total number of fields
        } else { # actual data
            my $n_commas = $_ =~s/,/,/g; # total number of commas
            foreach my $i (0 .. $n_commas - $n_fields) { # iterate ($n_commas - $n_fields + 1) times
                s/(\"[^",]+),([^"]+\")/$1\\x2c$2/g; # single replacement per previous answers
            }
            s/\"//g; # removal of double quotes (if you want)
        }
        print "$_\n";
    }
    

提交回复
热议问题