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


and convert it to


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

  • What do you mean be "including none" - that would just be the original word... onetwo-three would be onetwoThree – Kinnectus Jun 22 '16 at 11:23
  • @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 Jun 22 '16 at 11:28
  • @MichaelKjörling And split the words for him? Gee, I'll pass on this one. – MariusMatutiae Jun 22 '16 at 13:11
  • @MichaelKjörling - That's correct. If I were to pass in the string 'baseball' the solution must return that untouched ... – TheIcemanCometh Jun 22 '16 at 16:10

Perl to the rescue:

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

\u upcases the following character.

  • Works perfectly. Tested on MacOS (El Capitan). – TheIcemanCometh Jun 22 '16 at 16:11
  • Also tested on Debian Wheezy and works like a charm. – a CVn Jun 22 '16 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.