Since you're using bash
, if you stored your string in a variable you could also do it shell-only:
uscore="this_is_the_string_to_be_converted"
arr=(${uscore//_/ })
printf %s "${arr[@]^}"
ThisIsTheStringToBeConverted
${uscore//_/ }
replaces all _
with space, (....)
splits the string into an array, ${arr[@]^}
converts the first letter of each element to upper case and then printf %s ..
prints all elements one after another.
You can store the camel-cased string into another variable:
printf -v ccase %s "${arr[@]^}"
and use/reuse it later, e.g.:
printf %s\\n $ccase
ThisIsTheStringToBeConverted
Or, with zsh
:
uscore="this_is_the_string_to_be_converted"
arr=(${(s:_:)uscore})
printf %s "${(C)arr}"
ThisIsTheStringToBeConverted
(${(s:_:)uscore})
splits the string on _
into an array, (C)
capitalizes the first letter of each element and printf %s ...
prints all elements one after another..
To store it in another variable you could use (j::)
to joins the elements:
ccase=${(j::)${(C)arr}}
and use/reuse it later:
printf %s\\n $ccase
ThisIsTheStringToBeConverted
\U\2
inserts the found text from the second group, converted to ALL CAPS. Compare to\u\2
, which inserts the text in Sentence case, with only the first character capitalized. (2) All of the examples given below will translate “this_is_a_string” to “ThisIsAString” — which is what you asked for, but is slightly hard to read. You might want to revise your requirements for the special case of a one-letter word (substring). … (Cont’d) – Scott - Слава Україні Apr 14 '15 at 19:58(^|_)
to(\<|_)
. – Scott - Слава Україні Apr 14 '15 at 19:58