Inserting a space between each letter
Insert a space between each letter.
$ echo -e "hello life\nand 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 life\nand 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 life\nand 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."