Vano
Vano

Reputation: 740

Convert camelcase to lower and underscore_case using bash commands

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!

Upvotes: 5

Views: 2843

Answers (2)

Andrew Hershberger
Andrew Hershberger

Reputation: 4162

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

Upvotes: 5

PesaThe
PesaThe

Reputation: 7499

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

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

Upvotes: 8

Related Questions