本文介绍了使用 Vim 删除文件中的所有空格并用逗号替换它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何使用 Vim 在文件中删除所有空格并用逗号 , 替换它们?文件输入示例(单词可能无处不在!):

Anyone has an idea how to remove all white space and replace them with a comma , in a file using Vim ?File input example (words could be everywhere !):

C1       TEST   PROD
A1    BE


T1     B1

文件输出示例(属于同一行的所有单词如下例所示):

File output example(all words belonging to the same line are like in the example below):

C1,TEST,PROD
A1,BE
T1,B1

我找到了:%s/\s\{1,}/,/gc

推荐答案

先删除空行:

:g/^\s*$/d

然后在每一行 (%) 上使用替换 (:s///) 来替换所有 (g) 连续空格(\s\+) 和逗号 (,).

Then use a substitution (:s///) over each line (%) to replace all (g) continuous whitespace (\s\+) with a comma (,).

 :%s/\s\+/,/g

这篇关于使用 Vim 删除文件中的所有空格并用逗号替换它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:39
查看更多