Convert underscore to PascalCase, ie UpperCamelCase
$ echo "this_is_the_string" | sed -r 's/(^|_)([a-z])/\U\2/g'
ThisIsTheString
Substitute pattern
(^|_)
at the start of the string or after an underscore - first group
([a-z])
single lower case letter - second group
by
\U\2
uppercasing second group
g
globally.
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
Here's a Perl way:
$ echo "this_is_the_string" | perl -pe 's/(^|_)./uc($&)/ge;s/_//g'
ThisIsTheString
It can deal with strings of arbitrary length:
$ echo "here_is_another_larger_string_with_more_parts" |
perl -pe 's/(^|_)./uc($&)/ge;s/_//g'
HereIsAnotherLargerStringWithMoreParts
It will match any character (.
) that comes after either the start of the string or an underscore ((^|_)
) and replace it with the upper case version of itself (uc($&)
). The $&
is a special variable that contains whatever was just matched. The e
at the end of s///ge
allows the use of expressions (the uc()
function in this case) within the substitution and the g
makes it replace all occurrences in the line. The second substitution removes the underscores.