I'm trying to insert a space between letters only, not numbers or other characters.
hello woRLd 12ab34 should become
h e l l o w o R L d 12a b34
sed 's/\([a-zA-Z]\)\([a-zA-Z]\)/\1 \2/g' file.txt
h el lo w or LD 12a b34
I can't insert a space after every letter, as that doesn't check if the one after that will be a letter also.
I could run the
sed command twice, which solves the problem but is not elegant. I need to solve this problem using
sed, if possible.
You don't need to run the
sed command twice, you can simply run the
substitute command inside the
sed script twice.
sed has an elegant way to do this: The empty pattern
// repeats the previous pattern:
sed 's/\([a-zA-Z]\)\([a-zA-Z]\)/\1 \2/g;s//\1 \2/g' file.txt
For the sake of readability, I suggest to use extended regular expressions:
sed -E 's/([a-zA-Z])([a-zA-Z])/\1 \2/g;s//\1 \2/g' file.txt