Camel-casing a 'dashed' string

1

I'm fairly inexperienced with *nix scripting, and I have a need to take a string with lowercase words separated by dashes and convert that to a new string with the dashes removed, and the first letter after each original dash uppercased. For example, I want to take

one-two-three-four-five

and convert it to

oneTwoThreeFourFive

The solution must be able to handle multiple dashes (including none), and can use other *nix utilities if necessary like sed, awk, etc.

TheIcemanCometh

Posted 2016-06-22T11:20:54.993

Reputation: 113

What do you mean be "including none" - that would just be the original word... onetwo-three would be onetwoThree – Kinnectus – 2016-06-22T11:23:57.797

@BigChris I think OP simply means that the proposed solution cannot assume that any dashes are present at all, and must gracefully handle that case. – a CVn – 2016-06-22T11:28:38.060

@MichaelKjörling And split the words for him? Gee, I'll pass on this one. – MariusMatutiae – 2016-06-22T13:11:38.727

@MichaelKjörling - That's correct. If I were to pass in the string 'baseball' the solution must return that untouched ... – TheIcemanCometh – 2016-06-22T16:10:13.497

Answers

2

Perl to the rescue:

echo one-two-three-four-five | perl -pe 's/-(.)/\u$1/g'

\u upcases the following character.

choroba

Posted 2016-06-22T11:20:54.993

Reputation: 14 741

Works perfectly. Tested on MacOS (El Capitan). – TheIcemanCometh – 2016-06-22T16:11:38.643

Also tested on Debian Wheezy and works like a charm. – a CVn – 2016-06-22T19:44:58.023