Smarter Removing Unnecessary WhiteSpace CSV

浪子不回头ぞ 提交于 2020-01-14 03:23:27

问题


I have a comma separated file (CSV) that resembles the following

1, 2, 3, "Test, Hello"
4, 5, 6, "Well, Hi There!"

I need to be able to transform the above from a Linux command line ideally into

1,2,3,"Test, Hello"
4,5,6,"Well, Hi There!"

Now, I am aware of some of the other solutions like: Removing spaces after all commas

This, however was not aware of strings which were enclosed in double quotes. For example, the solution on the page:

sed -e 's/\s\+,/,/g'

Produces...

1,2,3,"Test,Hello"
4,5,6,"Well,Hi There!"

IT IS NOT THE SAME! This method removed the spaces within the enclosed string. Does anybody have an idea how to remove white spaces without destroying that which is enclosed in double quotes? Or if that is too difficult, a specific field instead?


回答1:


perl -lne 'if(/(.*?\")(.*)/){$b=$2;$a=$1;$a=~s/,[\s]/,/g;print "$a$b"}' your_file

Tested below:

> cat temp
1, 2, 3, "Test, Hello"
4, 5, 6, "Well, Hi There!"
>
> perl -lne 'if(/(.*?\")(.*)/){$b=$2;$a=$1;$a=~s/,[\s]/,/g;print "$a$b"}' temp
1,2,3,"Test, Hello"
4,5,6,"Well, Hi There!"
> 

Or you can use awk (i used nawk since i am working on solaris):

nawk -F'\"' -v OFS='\"' '{gsub(/ /,"",$1)}1' your_file


来源:https://stackoverflow.com/questions/17206556/smarter-removing-unnecessary-whitespace-csv

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