Convert underscore to PascalCase, ie UpperCamelCase

If I have a string that looks like this:

"this_is_the_string"

Inside a bash script, I would like to convert it to PascalCase, ie UpperCamelCase to look like this:

"ThisIsTheString"

I found that converting to lowerCamelCase can be done like this:

"this_is_the_string" | sed -r 's/([a-z]+)_([a-z])([a-z]+)/1U2L3/'

Unfortunately I am not familiar enough with regexes to modify this.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

$ echo "this_is_the_string" | sed -r 's/(^|_)([a-z])/U2/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
U2 uppercasing second group
g globally.

Method 2

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.

Method 3

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

Method 4

It is not necessary to represent the entire string in a regular expression match — sed has the /g modifier that allows you to walk over multiple matches and replace each of them:

echo "this_is_the_string" | sed 's/_([a-z])/U1/g;s/^([a-z])/U1/g'

The first regex is _([a-z]) — each letter after underscore; the second one matches the first letter in a string.

Method 5

I only put in this answer because it is shorter and simpler than any other so far.

sed -re "s~(^|_)(.)~U2~g"

It says: upcase, the character following a _ or the start. Non letters will not be changed, as they have no case.

Method 6

In perl:

$ echo 'alert_beer_core_hemp' | perl -pe 's/(?:b|_)(p{Ll})/u$1/g'
AlertBeerCoreHemp

This is also i18n-able:

$ echo 'алерт_беер_коре_хемп' | perl -CIO -pe 's/(?:b|_)(p{Ll})/u$1/g'
АлертБеерКореХемп

Method 7

I did it this way:

echo "this_is_the_string" | sed -r 's/(<|_)([[:alnum:]])/U2/g'

and got this result:

ThisIsTheString

Method 8

My choice is:

echo "this_is-the_string-2.0" |  perl -pe 's/(?:^|[^a-z])([a-z0-9])/u$1/g'

Which results in:

ThisIsTheString20


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x