问题
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