Using GNU sed
, (and bash
):
F="my name is user from here not there."
sed -E 's/^./\u&/;s/([[:space:]])([[:alpha:]]{4})/\1\u\2/g' \
<<< "${F,,}"
or:
sed -E 's/^./\u&/;s/(\s)(\w{4})/\1\u\2/g' <<< "${F,,}"
Output:
My Name is User From Here not There.
Notes:
"${F,,}"
is a bash
case modification parameter expansion, it returns a lower-case version of $F
, which becomes the input for sed
.
GNU sed
offers some useful synonyms and abbreviations for common regex character classes. The character class [a-zA-Z0-9_]
can be abbreviated as [[:alpha:]_]
, or simpler yet \w
.
Even though \u
looks like a regex abbreviation, it's not. It's a "special sequence" used only in s
ubstitute command replacement text -- \u
means "turn the next character to uppercase".
&
refers to whatever the first regexp in the s
ubstitute command matched. Compare the following:
sed 's/./&/' <<< foo # outputs "f"
sed 's/./&/g' <<< foo # outputs "foo"
sed 's/./&&&&/g' <<< foo # outputs "ffffoooooooo"
sed 's/./\u&&&\u&/g' <<< foo # outputs "FffFOooOOooO"
sed 's/.*/&&&&/' <<< foo # outputs "foofoofoofoo"
See the GNU sed
info pages for more details.