简体   繁体   English

perl oneliner搜索替换模式

[英]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). 我正在寻找一种Perl线性代码,可以删除引号(第二列)中可能出现的任何逗号。 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;' perl -ne 's/(?<!,),//g; print;' But I can't figure out how to keep the other commas. 但是我不知道如何保留其他逗号。

Easy using Text::CSV_XS : 易于使用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. 根据Borodin评论脚本的更新。 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) . 带有非破坏性修饰符(r) Non-destructive modifier will work on only > 5.14. 非破坏性修改器仅在> 5.14上有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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