我有一个变量(称为$document_keywords),其中包含以下文本:
Latex document starter CrypoServer
我想在每个单词之后添加逗号,而不是在最后一个单词之后.所以,输出将变成这样:
Latex, document, starter, CrypoServer
有人帮我实现上述输出.
问候,
ANKIT
解决方法:
为了保留空格,我会像这样使用sed:
echo "$document_keywords" | sed 's/\>/,/g;s/,$//'
其工作原理如下:
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.
然后:
$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