Inserting a space between each letter

September 2017






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

Published by jak58. Latest update on November 6, 2016 at 03:48 PM by owilson.
This document, titled "Inserting a space between each letter," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).