Remove double quotes and comma from a numeric value of a .CSV file

寵の児 提交于 2019-12-04 17:01:29

To remove the quotes (replace the number with the quotes with the number without them):

s/"(\d[\d.,]*)"/\1/g

See on rubular

For the commas I could only think of a lookahead and lookbehind, if thats supported by your regex implementation (replace commas with nothing if before and after is a number within quotes):

s/(?<="[\d,]+),(?=[\d,.]+")//g

You would have to execute this before removing the quotes.

It might also work without lookbehind:

s/,(?=[\d,.]*\d")//g

See on rubular

In a shell script you might want use perl e.g. execute:

cat test.csv | perl -p -e 's/,(?=[\d,.]*\d")//g and s/"(\d[\d,.]*)"/\1/g'

Explanation of the regex:

first execute:

s/,(?=[\d,.]*\d")//g 

This will remove all commas that are followed by a number ([\d,.]*\d) and a quote, thus removing only commas from numbers within quotes

next execute

s/"(\d[\d,.]*)"/\1/g

This will replace all numbers that are within quotes by the value without the quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!