从CSV文件的数字字段中删除逗号

时间:2016-07-30 15:05:32

标签: perl

我有一个如下输入文件:

13/9/16,"71,150","49,122","13,212","3,135","3,652","140,271",0,Pull,301 Off,Standard,0,"6,237",43,53,0,"6,141","3,641","2,500","25,353","4,136","4,004",335,89,"1,909",312,382,49,183,0,"44,778"
14/9/16,"72,799","53,420","13,130","3,359","3,798","146,506",0,Pull,301 Off,Standard,0,"6,779",45,56,0,"6,678","4,178","2,500","25,940","4,232","3,979",349,96,"1,953",319,409,51,190,0,"46,525"
15/9/16,"97,532","16,861","12,386","6,066","17,323","150,168",F&F,+24,301 On,Standard,0,"2,413",203,102,0,"2,108","1,054","1,054","34,753","5,670","3,754","1,588",172,"2,616",427,739,231,867,0,"49,546"

我正在尝试删除以双引号,开头和结尾的字符串中的逗号"

所以例如第1行有字符串"71,150"我希望它看起来像"71150",其余的第1行围绕它。

所以第1行的完整输出如下所示:

13/9/16,"71150","49122","13212","3135","3652","140271",0,Pull,301 Off,Standard,0,"6237",43,53,0,"6141","3641","2500","25353","4136","4004",335,89,"1909",312,382,49,183,0,"44778"

我在考虑像

这样的东西
s/\,//g

但这将替换整个文件中的所有逗号。

我知道我可以使用以下代码行来查找我想要更改的字符串:

/\"[0-999]\,[0-999]\"/

但我不知道从哪里去。

2 个答案:

答案 0 :(得分:4)

如果数字字段中只有一个逗号,则可以使用

perl -pe 's/"\d+\K,//g' input.txt

输出

13/9/16,"71150","49122","13212","3135","3652","140271",0,Pull,301 Off,Standard,0,"6237",43,53,0,"6141","3641","2500","25353","4136","4004",335,89,"1909",312,382,49,183,0,"44778"
14/9/16,"72799","53420","13130","3359","3798","146506",0,Pull,301 Off,Standard,0,"6779",45,56,0,"6678","4178","2500","25940","4232","3979",349,96,"1953",319,409,51,190,0,"46525"
15/9/16,"97532","16861","12386","6066","17323","150168",F&F,+24,301 On,Standard,0,"2413",203,102,0,"2108","1054","1054","34753","5670","3754","1588",172,"2616",427,739,231,867,0,"49546"

答案 1 :(得分:1)

perl -pe 's/("\d+),(\d+")/$1$2/g' infile.txt
  • "\d+双引号后跟一个或多个数字
  • ,匹配逗号
  • \d+"一个或多个数字后跟双引号
  • ()分组用于捕获匹配的模式
  • $1首次捕获的小组
  • $2第二个被捕获的小组

如果要更改文件,请使用

perl -i.bkp -pe 's/("\d+),(\d+")/$1$2/g' infile.txt

将更改infile.txt并创建原始文件的备份infile.txt.bkp