LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2006, 06:52 AM   #1
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-19-2006, 08:52 AM   #2
pronoy
Member
 
Registered: Mar 2005
Location: india
Distribution: FC4, Debian Etch
Posts: 67

Rep: Reputation: Disabled
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:]'
 
Old 05-19-2006, 08:53 AM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 05-19-2006, 09:37 AM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

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

Last edited by crabboy; 05-19-2006 at 09:39 AM.
 
Old 05-19-2006, 10:38 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Original Poster
Rep: Reputation: 148Reputation: 148
Thank you all very much I've now got it working the way I wanted.
 
Old 04-06-2010, 12:13 PM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,623

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Cool 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.
 
Old 04-06-2010, 02:08 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by wpeckham View Post
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+?
 
Old 04-06-2010, 03:48 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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#_}"
 
Old 04-06-2010, 08:03 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Original Poster
Rep: Reputation: 148Reputation: 148
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
 
Old 04-06-2010, 08:48 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
  


Reply

Tags
awk, case



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM
Change Case DavidPhillips Programming 6 04-02-2005 04:45 PM
The case of the disappearing bash history geomatt Linux - General 5 03-02-2005 04:08 PM
change case in perl? bigearsbilly Programming 3 01-20-2005 07:08 AM
Bash case structure (looping) ravvar Programming 4 10-07-2003 07:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration