0
Thanks

A few words of thanks would be greatly appreciated.

Inserting a space between each letter






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."
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team

Published by . Latest update on by Olivia Long.

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 (https://ccm.net/).

0 Comments