Convert camelcase to lower and underscore_case using bash commands

Viewed 1803

I am trying to convert camelcase string into lower and underscore case, I don't know many bash commands and I couldn't find anything, if this doesn't take big time for you, please give me example. like: 'TestCamelCase' -> result should be 'test_camel_case'. thank you in advanced!

2 Answers
$ sed 's/^[[:upper:]]/\L&/;s/[[:upper:]]/\L_&/g' <<< 'TestCamelCase'
test_camel_case

\L is a GNU sed extension that turns the replacement to lowercase.

Here's an approach that avoids the need for GNU sed extensions and works on macOS:

$ echo 'TestCamelCase' | sed 's/[[:upper:]]/_&/g;s/^_//' | tr '[:upper:]' '[:lower:]'
test_camel_case
Related