Monday, April 11, 2011

How can I cut(1) camelcase words?

Is there an easy way in Bash to split a camelcased word into its constituent words?

For example, I want to split aCertainCamelCasedWord into 'a Certain Camel Cased Word' and be able to select those fields that interest me. This is trivially done with cut(1) when the word separator is the underscore, but how can I do this when the word is camelcased?

From stackoverflow
  • sed 's/\([A-Z]\)/ \1/g'

    Captures each capital letter and substitutes a leading space with the capture for the whole stream.

    $ echo "aCertainCamelCasedWord" | sed 's/\([A-Z]\)/ \1/g'
    a Certain Camel Cased Word
    
    Christopher W. Allen-Poole : I love regular expressions.
  • Pure Bash:

    name="aCertainCamelCasedWord"
    
    declare -a word                                 # the word array
    
    counter1=0                                      # count characters
    counter2=0                                      # count words
    
    while [ $counter1 -lt ${#name} ] ; do
      nextchar=${name:${counter1}:1}
      if [[ $nextchar =~ [[:upper:]] ]] ; then
        ((counter2++))
        word[${counter2}]=$nextchar
      else
        word[${counter2}]=${word[${counter2}]}$nextchar
      fi
      ((counter1++))
    done
    
    echo -e "'${word[@]}'"
    
    Judge Maygarden : Interesting, yet much more verbose. Use the right tool for the job I say! ;)

0 comments:

Post a Comment