Inserting a space between each letter

December 2016






Insert a space between each letter.

$ echo -e "hello lifenand hello to you" | sed 's/./& /g'
h el l o l i f e
a n d h e l l o t o y o u
  • The problem is that the natural space between each word is doubled. To solve this just add:



$ echo -e "hello lifenand hello to you" | sed 's/./& /g;s/  / /g'
h el l o l i f e
a n d h e l l o t o y o u
$
  • Here is a variant. Compared with the previous replacement and done in a single shot s///

$ echo -e "hello lifenand hello to you" | sed -r 's/([^ ])/1 /g'
h el l o l i f e
a n d h e l l o t o y o u
  • If you do not want to use the-r option


sed 's/([^ ])/1 /g'


[^...] class character
  • [^...] - Is a character class complemented which means "to recognize a non-listed character", do not sonfuse with "not to recognize a listed character.
  • In our case can be translated "to recognize a character that is not space."

Related :

This document entitled « Inserting a space between each letter » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.