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


  • sed 's/[[:blank:]]\+/,/g' – mug896 Dec 25 '15 at 12:55
  • @mug896 worked for me on Ubuntu 18.04 I needed the double square brackets and the \ before the + – dmontaner Mar 5 '20 at 11:04

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'.

  • Thanks, however there is still an issue: I don't want to replace "next lines \n" with a comma, and the command with perl is currently doing it. She sed command you wrote, it still doesn't work. – aneuryzm Apr 22 '11 at 8:29
  • Sorry you need to escape the + operator in sed, I just updated my answer. Newlines are whitepsace in perl, so you will have to do your whitepsace class manually if you don't wnat to include it. I added two solutions for that as wel. – Caleb Apr 22 '11 at 8:41

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.


The issue with your command,

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

is twofold:

  1. The [:blank:] is a bracketed expression matching one of the characters :, a, b, k, l, or n. If you want to match one of the characters in the POSIX character class [:blank:], use [[:blank:]].

  2. The + is an extended regular expression modifier. The sed utility uses basic regular expressions, and the + would match a literal + character. To get the same effect in a basic regular expression, use \{1,\}, or, in this particular expression, use [[:blank:]][[:blank:]]* instead.

In short, your corrected sed expression would be

sed 's/[[:blank:]]\{1,\}/,/g' orig.txt >modified.txt


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

As others have pointed out, and assuming single byte ASCII characters in the text, it may be more efficient to use tr in this case, as it's a simple transliteration of one set of characters to another. Either one of the following two tr commands would solve the issue:

tr -s '[:blank:]' '[,*]' <orig.txt >modified.txt

where [,*] means "as many commas as is needed for this set to match the number of characters in the first set", or

tr -s '\t ' ',,' <orig.txt >modified.txt

The -s option to tr causes multiple consecutive commas to be "squeezed" into single commas.

  • 1
    Note that some tr implementations including GNU tr only work correctly with single-byte characters, so they would fail to handle blank characters such as U+1680 OGHAM SPACE MARK, U+2000 EN QUAD, U+2001 EM QUAD, U+2002 EN SPACE, U+2003 EM SPACE, U+2004 THREE-PER-EM SPACE, U+2005 FOUR-PER-EM SPACE, U+2006 SIX-PER-EM SPACE, U+2008 PUNCTUATION SPACE, U+2009 THIN SPACE, U+200A HAIR SPACE, U+205F MEDIUM MATHEMATICAL SPACE, U+3000 IDEOGRAPHIC SPACE in those locales where those characters are encoded on more than one byte (like those using UTF-8 as their charmap)... – Stéphane Chazelas Mar 6 at 11:45
  • @StéphaneChazelas Hair space? I lack a philosophical space... But much thanks, as usual. I keep on learning. – Kusalananda Mar 6 at 11:48
  • @StéphaneChazelas Useful! Thanks! – Kusalananda Mar 6 at 11:50
  • ~$ vim filename
  • :set nu (just for setting lines)
  • :%s/ /,/g (this will replace space to comma)
  • :wq! (save and exit)
  • 2
    Note that the user wants to also replace tabs ("all white-spaces"). It is clear from their attempted command that they don't want multiple consecutive commas in the resulting text, even if there are multiple consecutive spaces or tabs in the original. Also consider formatting your answer using the tools provided. – Kusalananda Mar 6 at 11:33
awk '{print $1","}' Servers | tr -d '[\n]' | xargs echo 

Here the file name is Servers.. It contains the below content..


If we use the above command we can get the below output..


[mohramak@oc3246038448 Desktop]$ cat Servers
[mohramak@oc3246038448 Desktop]$ awk '{print $1","}' Servers | tr -d '[\n]' | xargs echo 
[mohramak@oc3246038448 Desktop]$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.