6

我有一个看起来像这样的记录集

"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"

我想,用管道( )替换逗号( |)而不替换里面的逗号

"Leigh & Bethie,Coles"
"Waynez,Jessy & Lyne"
"Bronwynie, Paula & Arianne"

如何使用正则表达式或其他方法做到这一点?

4

3 回答 3

12

你不能用正则表达式来做;您可以使用适当的 CSV 解析器来完成。这是一个使用Text::CSV_XS的(未经测试的)示例- 业内最好的。

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 );
}
于 2013-06-25T23:44:09.863 回答
6

只是为了 TIMTOWTDI,这里是一个使用核心模块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"
于 2013-06-26T03:09:38.837 回答
0

如何利用逗号出现的上下文(在双引号之间):

s/","/"|"/g
于 2013-06-25T23:40:28.503 回答