简体   繁体   中英

perl oneliner search replace pattern

I have thousands of rows where some contain the following:

id_s,title_dk
KKS2826,"Søslag ved Øland og Gulland, 1564",12312,2x2
KKS935,"Vignet til Edvard Brandes, afhandling om Johan Wiehe", 1233, 4x4

I'm looking for a Perl one-liner where I can delete any comma that might occur within quotations (the second column). But obviously not the others, wince they are delimiters.

So desired output would be:

id_s,title_dk
KKS2826,"Søslag ved Øland og Gulland 1564",12312,2x2
KKS935,"Vignet til Edvard Brandes afhandling om Johan Wiehe", 1233, 4x4

I have been playing with this: perl -ne 's/(?<!,),//g; print;' perl -ne 's/(?<!,),//g; print;' But I can't figure out how to keep the other commas.

Easy using Text::CSV_XS :

perl -CS -MText::CSV_XS=csv -we '
    my $aoa = csv(in => shift, allow_whitespace => 1);
    $_->[1] =~ s/,//g for @$aoa;
    csv(in => $aoa, out => *STDOUT, always_quote => 0);
    ' input.csv > output.csv

Try this one liner

perl -p -e  's/"([^"]*)"/my $m=$1;$m=~ s:,::g; $m /eg' file.txt

As per Borodin comment script updated. Because above script will remove the " also.

perl -p -e  's/ (?<=") ([^"]*) (?<=")/$1=~ s:,::rg; /xeg' file.txt

In second one, I used positive look ahead and look behind. With non-destructive modifier (r) . Non-destructive modifier will work on only > 5.14.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM