问题描述
我有一个类似于以下内容的逗号分隔文件(CSV):
1,2,3,
4,5,6,你好!
我需要能够将上述内容从Linux命令行转换为
1,2,3,Test,Hello
4,5,6,你好!
现在,我注意到其他一些解决方案:
这样,但是不知道包含在双引号中的字符串。例如,页面上的解决方案:
sed -e'/ \s\ +,/,/ g '
产生...
1,2,3,测试,你好
4,5,6,你好!
它不一样!此方法删除包含的字符串中的空格。任何人都有一个想法如何删除白色空间,而不破坏包含在双引号?
$ perl -lne'如果这太难了, if(/(.*?\)(。*)/){$ b = $ 2; $ a = $ 1; $ a =〜s /,[\ s] /,/ g; print$ a $ b}'your_file
测试如下:
> cat temp
1,2,3,Test,Hello
4,5,6,Well,Hi There! b>
> perl -lne'if(/(.*?\)(。*)/){$ b = $ 2; $ a = $ 1; $ a =〜s / \\ s] /,/ g; print$ a $ b}'temp
1,2,3,Test,Hello
4,5,6,Hi there!
>
或者你可以使用awk(我使用nawk,因为我正在solaris):
nawk -F'\'-v OFS ='\''{gsub(/ /,,$ 1)} 1'your_file
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?
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
这篇关于更智能地删除不必要的WhiteSpace CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!