替换文件中的空格而不更改字段中的空格

Replace spaces in a file without changing those within fields

我有一个 CSV 文件,其中包含

5213 , 1395429467 , A , r7.sn-a8au-hp5e.gvt1.com , 4.31.38.18 , 1460706280 , John Doe

我知道如何替换文件中的空格::%s/ //g。它会更改包含空格的整个文件。

我希望最后一个元素 </code> 保留其空间,因此 <code>John Doe 必须保持 John Doe 而不是 JohnDoe.

总的来说,预期的输出是:

5213,1395429467,A,r7.sn-a8au-hp5e.gvt1.com,4.31.38.18,1460706280,John Doe

有人可以帮助我实现这一目标吗?

您可能只需要将每个 ,(即 space + 逗号 + space)替换为单个 ,:

sed 's/ , /,/g' file

重播:

5213,1395429467,A,r7.sn-a8au-hp5e.gvt1.com,4.31.38.18,1460706280,John Doe