How to replace space with comma using sed? How to replace space with comma using sed? unix unix

How to replace space with comma using sed?


If you are talking about sed, this works:

sed -e "s/ /,/g" < a.txt

In vim, use same regex to replace:

s/ /,/g


Inside vim, you want to type when in normal (command) mode:

:%s/ /,/g

On the terminal prompt, you can use sed to perform this on a file:

sed -i 's/\ /,/g' input_file

Note: the -i option to sed means "in-place edit", as in that it will modify the input file.


I know it's not exactly what you're asking, but, for replacing a comma with a newline, this works great:

tr , '\n' < file