简体   繁体   English

使用sed从字符串中删除美元符号和逗号

[英]Remove Dollar signs and commas from a string using sed

sorry if this has been posted in some form already. 抱歉,如果此消息已经以某种形式发布。 I cannot find anything that exactly answers my question. 我找不到任何能完全回答我问题的东西。

Im new to awk, sed, and bash. 我是awk,sed和bash的新手。 And I need to figure this out for a class project. 我需要为一个班级项目弄清楚这一点。

I have a text file separated by commas, with all the data inside double quotes. 我有一个用逗号分隔的文本文件,所有数据都用双引号引起来。

Example piece: 样例:

,"CHADWICK R GORE","C00538975","2012","$10,000.00",,"$10,000.00",,,"$5,000.00",,,"$15,000.00",,,,,,,,,,,,,"$15,000.00","$15,000.00",,,,

I need to remove the dollar signs and commas from the fields with money in them. 我需要从里面的钱中删除美元符号和逗号。 I used sed to do it. 我用sed来做。 My sed code: 我的sed代码:

s/"\$\([^,"]*\),/"$\1/g 
s/"\$\([^"]*\)"/\1/g

This seems to be wrong, and isn't getting rid of all the commas if there are two in the number. 这似乎是错误的,并且如果数字中有两个,也不会消除所有逗号。

Any help would be appreciated! 任何帮助,将不胜感激! Thanks 谢谢

this line does the job: 这行工作:

awk -F'"' -v OFS="\"" '{for(i=1;i<=NF;i++)if($i~/^\$[0-9.,]+$/)gsub(/[$,]/,"",$i)}1'

the output from your example text is: 您的示例文本的输出为:

,"CHADWICK R GORE","C00538975","2012","10000.00",,"10000.00",,,"5000.00",,,"15000.00",,,,,,,,,,,,,"15000.00","15000.00",,,,

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

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