使用命令行删除逗号前后的空格(不在单词之间) [英] Removing spaces after and before commas (not between words) using command line

查看:171
本文介绍了使用命令行删除逗号前后的空格(不在单词之间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用pull_data $a $b > $id.csv创建的CSV文件.

I have a CSV file that is created using pull_data $a $b > $id.csv.

例如输出中的一行是:

1041894,30/01/2013,31/01/2013,A Customer Limited , 2, 1,PR14, PR14 , 104 An Item ,247 An Item

这是我的病情:

  • 我需要删除逗号前后的所有空格,但要排除单词之间的空格.

  • I need to remove all spaces after or before the commas but exclude spaces that are between words.

我需要每一行看起来像下面的样子,如果可能的话,我想使用sed:

I need each line to look like the below and would like to use sed if possible:

1041894,30/01/2013,31/01/2013,A Customer Limited,2,1,PR14,PR14,104 An Item,247 An Item

我尝试过sed 's/, \+\| \+,/,/g',但这不会删除逗号之后或之前的所有空格,只会删除一些空格.

I have tried sed 's/, \+\| \+,/,/g' but this does not remove all the spaces after or before a comma, it removes only some.

推荐答案

尝试此sed:

sed -i.bak -e 's/,[[:space:]]\+/,/g' -e 's/[[:space:]]\+,/,/g' file

或者这个awk:

awk -F '[[:space:]]*,[[:space:]]*' '{$1=$1}1' OFS=, file

更新:

OP可能不仅具有空格,还具有空白:

也尝试此sed:

sed -i.bak 's/[[:space:]]*,[[:space:]]*/,/g' file

这篇关于使用命令行删除逗号前后的空格(不在单词之间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆