Search code examples
linuxbashcamelcasing

linux bash, camel case string to separate by dash


Is there a way to convert something like this:

MyDirectoryFileLine

to

my-directory-file-line

I found some ways to convert all letters to uppercase or lowercase, but not in that way; any ideas?


Solution

  • 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"