linux bash, camel case string to separate by dash

You can use s/\([A-Z]\)/-\L\1/g to find an upper case letter and replace it with a dash and it’s lower case. However, this gives you a dash at the beginning of the line, so you need another sed expression to handle that.

This should work:

sed --expression 's/\([A-Z]\)/-\L\1/g' \
    --expression 's/^-//'              \
    <<< "MyDirectoryFileLine"

Leave a Comment