Replace all white spaces with commas in a text file
With 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'
.
Using tr
:
tr -s '[:blank:]' ',' <file
This will replace any horizontal whitespace with a comma. Any repeated whitespace will only be replaced with a single comma.