I have a variable (called $document_keywords) with following text in it:
Latex document starter CrypoServer
I want to add comma after each word, not after last word. So, output will become like this:
Latex, document, starter, CrypoServer
Anybody help me to achieve above output.
regards, Ankit
In order to preserve whitespaces as they are given, I would use sed like this:
echo "$document_keywords" | sed 's/\>/,/g;s/,$//'
This works as follows:
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.
Then:
$ 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
A normal sed gave me the expected output,
sed 's/ /, /g' filename
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With