在每个单词后添加逗号

Ank*_*ani 4 linux shell escaping sed comma

我有一个变量(称为$ document_keywords),其中包含以下文本:

Latex document starter CrypoServer
Run Code Online (Sandbox Code Playgroud)

我想在每个单词之后添加逗号,而不是在最后一个单词之后.所以,输出将变成这样:

Latex, document, starter, CrypoServer
Run Code Online (Sandbox Code Playgroud)

有人帮我实现上述输出.

问候,Ankit

Win*_*ute 5

为了保留空格,我会像这样使用sed:

echo "$document_keywords" | sed 's/\>/,/g;s/,$//'
Run Code Online (Sandbox Code Playgroud)

其工作原理如下:

s/\>/,/g   # replace all ending word boundaries with a comma -- that is,
           # append a comma to every word
s/,$//     # then remove the last, unwanted one at the end.
Run Code Online (Sandbox Code Playgroud)

然后:

$ echo 'Latex document starter CrypoServer' | sed 's/\>/,/g;s/,$//'
Latex, document, starter, CrypoServer
$ echo 'Latex   document starter CrypoServer' | sed 's/\>/,/g;s/,$//'
Latex,   document, starter, CrypoServer
Run Code Online (Sandbox Code Playgroud)