LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: change case (http://www.linuxquestions.org/questions/programming-9/bash-change-case-446382/)

graemef 05-19-2006 06:52 AM

Bash: change case
 
Hi all, this request essentially illustrates my weakness with shell scripts. I have a script that reads in a string that will be in CamelCase and I want to convert the value into first lowercase and then uppercase with an underscore inserted at each Capital letter, after the first appearance, hence:

TestCaseVariable

will become:

test_case_variable

and

TEST_CASE_VARIABLE

I'm sure that there is an easy way to do this in bash, but I need someone to help me find it, many thanks.

pronoy 05-19-2006 08:52 AM

Can do it with 'sed'. Try like:

1. for test_case_variable
echo "TestCaseVariable" | sed -e 's/\([A-Z]\)/\_\1/g' -e 's/\_//' | tr '[:upper:]' '[:lower:]'

2. for TEST_CASE_VARIABLE
echo "TestCaseVariable" | sed -e 's/\([A-Z]\)/\_\1/g' -e 's/\_//' | tr '[:lower:]' '[:upper:]'

raskin 05-19-2006 08:53 AM

It is not to be done in bash itself. It is to be passed to sed (man sed).
a=$(sed -e 's/\([A-Za-z]\)\([A-Z]\)/\l\1_\2/g' <<<$b);
will probably do. There's L and one there, be careful.

crabboy 05-19-2006 09:37 AM

Code:

echo HelloThere | awk ' { gsub("[A-Z]","_&"); print }' | tr '[a-z]' '[A-Z]'

graemef 05-19-2006 10:38 AM

Thank you all very much I've now got it working the way I wanted.

wpeckham 04-06-2010 12:13 PM

Sed?
 
Actually there are two other ways to do this 'better' than with sed. Using psed or a single line of perl works, but calling tr is faster and uses less memory than either sed or perl.

I believe that the most recent versions of ksh have some of this capability built in. It may be only a matter of time before it is implemented in bash as an internal.

catkin 04-06-2010 02:08 PM

Quote:

Originally Posted by wpeckham (Post 3926410)
It may be only a matter of time before it is implemented in bash as an internal.

You're right! Folding case is in the latest bash as documented here (scroll down to the last expansions). IDK which version it's in, maybe 4+?

tuxdev 04-06-2010 03:48 PM

Code:

You're right! Folding case is in the latest bash as documented here (scroll down to the last expansions). IDK which version it's in, maybe 4+?
It is indeed a 4.x feature, however it's not useful for going from TestCaseVariable to test_case_variable. The problem is in inserting the _ properly. It is, however, useful for going from test_case_variable to TEST_CASE_VARIABLE.
Code:

var="TestCaseVariable"
var="${var//A/_a}"
var="${var//B/_b}"
var="${var//B/_c}"
...
var="${var//X/_x}"
var="${var//Y/_y}"
var="${var//Z/_z}"
var="${var#_}"


graemef 04-06-2010 08:03 PM

I don't recall what the original problem was (four years on) but I can assure you that it is no long keeping me up at nights ;)

grail 04-06-2010 08:48 PM

Hey I know you found a solution but I thought I would just throw 1 more:)

Code:

a=$(echo TestCaseVariable | sed 's@\(\B[A-Z]\)@_\1@g')

echo ${a^^} # uppercase
echo ${a,,} # lowercase

Last 2 are bash 4+ I believe.


All times are GMT -5. The time now is 05:36 PM.