Add space before uppercase letter
Using sed
, and assuming you don't want a space in front of the word:
$ sed 's/\([^[:blank:]]\)\([[:upper:]]\)/\1 \2/g' file.in
Add Data
Test Something
Tell Me Who You Are
The substitution will look for an upper-case letter immediately following a another non-whitespace character, and insert a space in-between the two.
For strings with more than one consecutive upper-case character, like WeAreATeam
, this produces We Are ATeam
. To sort this, run the substitution a second time:
$ sed -e 's/\([^[:blank:]]\)\([[:upper:]]\)/\1 \2/g' \
-e 's/\([^[:blank:]]\)\([[:upper:]]\)/\1 \2/g' file.in
Perl, using lookbehind and lookahead zero-width regular expressions:
$ perl -pe 's/(?<=\w)(?=[A-Z])/ /g' file.in
Tell Me Who You Are ## TellMeWhoYouAre
I Am A Regular Expression User ## IAmARegulaExpressionUser
This version is also separating consecutive uppercase letters.
sed -r -e "s/([^A-Z])([A-Z])/\1 \2/g"
Add space between a letter that is not an upper-case letter and a letter that is an upper-case letter