Replace all white spaces with commas in a text file

regular expressionsedtext processing

I need to replace all white spaces inside my text with commas. I'm currently using this line but it doesn't work: I get as output a text file which is exactly the same of the original one:

sed 's/[:blank:]+/,/g' orig.txt > modified.txt

thanks

Best Answer

With GNU sed:

sed -e 's/\s\+/,/g' orig.txt > modified.txt

Or with perl:

perl -pne 's/\s+/,/g' < orig.txt > modified.txt

Edit: To exclude newlines in Perl you could use a double negative 's/[^\S\n]+/,/g' or match against just the white space characters of your choice 's/[ \t\r\f]+/,/g'.

Related Question