如何从带引号的 csv 文件中删除逗号?

如何从带引号的 csv 文件中删除逗号?

我的输入是:

Name, Country, City
Jason, US, Memphis, "1,000"
David, US, Little Rock, "8,765,453"
"Karam, Sage", US, Nazareth, "4,678"
"David, simon", US, Chicago, "1,234"

我想要输出为:

Name, Country, City
Jason, US, Memphis, "1000"
David, US, Little Rock, "8765453"
"Karam, Sage", US, Nazareth, "4678"
"David, simon", US, Chicago, "1234"

我希望仅从第四列而不是第一列中删除逗号。

答案1

或者使用 perl:

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

它只是删除所有由数字 (\d) 包围的逗号。

我认为删除千位分隔符是您任务的本质 - 而不是逗号位于哪一列。

答案2

假设第四个提交是您的最后一个提交,那么一种AWK方法就是。

awk -F\" '{gsub(",","",$(NF-1))}1' OFS=\" infile.txt

输出是:

Name, Country, City
Jason, US, Memphis, "1000"
David, US, Little Rock, "8765453"
"Karam, Sage", US, Nazareth, "4678"
"David, simon", US, Chicago, "1234"

答案3

sed

sed -e ':a' -e 's/,\([^"]*"$\)/\1/;ta' infile.txt

只要倒数第二个双引号后面有一个要删除的逗号,就可以循环。

答案4

我建议您使用 CSV 解析器来解析 CSV 数据:ruby 有一个

ruby -rcsv -e '
  data = CSV.read(ARGV.shift, :col_sep => ", ")
  out = CSV.new($stdout, :col_sep => ", ")
  data.each {|row| row[-1].delete! ","; out << row}
' file.csv
Name, Country, City
Jason, US, Memphis, 1000
David, US, "Little Rock", 8765453
"Karam, Sage", US, Nazareth, 4678
"David, simon", US, Chicago, 1234

相关内容