Replace commas with pipes, but not the commas enclosed in double quotes

后端 未结 3 1122
庸人自扰
庸人自扰 2021-01-05 13:11

I have a record set that looks like this

\"BOSW0001\",\"Mr\",\"Wayne\",\"Boswell\",\"Wayne,Jessica & Lyn\",\"31          


        
相关标签:
3条回答
  • 2021-01-05 13:47

    How about utilizing the context in which the comma appears (between double-quotes):

    s/","/"|"/g
    
    0 讨论(0)
  • 2021-01-05 14:03

    Just for the sake of TIMTOWTDI, here is an example using the core module Text::ParseWords.

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    use Text::ParseWords 'parse_line';
    
    foreach my $line (<DATA>) {
      print join '|', parse_line(',', 1, $line);
    }
    
    __DATA__
    "BOSW0001","Mr","Wayne","Boswell","Wayne,Jessica & Lyn","31 Baker St"
    "ELLI0007","Mrs","Bronwyn","Elliott","Bronwyn, Paul & Arianne","98A Dandaraga Rd"
    "KENN0001","Mr","Leigh","Kenning","Leigh & Beth,Cole","22 Lake St"
    
    0 讨论(0)
  • 2021-01-05 14:08

    You don't do it with a regular expression; you do it with a proper CSV parser. Here's an (untested) example using Text::CSV_XS - the best in the biz.

    use strict;
    use warnings;
    
    use Text::CSV_XS;
    
    my $in_file = "whatever.csv";
    my $out_file = "new.dat";
    
    open my $fh, '<', $in_file or die "$in_file: $!";
    open my $out_fh, '>', $out_file or die "$out_file: $!";
    
    my $in_csv = Text::CSV_XS->new;
    my $out_csv = Text::CSV_XS->new( { sep_char => '|', eol => "\n" } );
    
    while( my $row = $in_csv->getline( $fh ) ) { 
        $out_csv->print( $out_fh, $row );
    }
    
    0 讨论(0)
提交回复
热议问题