LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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,376

Rep: Reputation: 147Reputation: 147
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: 15
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: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
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
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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,376

Original Poster
Rep: Reputation: 147Reputation: 147
Thank you all very much I've now got it working the way I wanted.
 
Old 04-06-2010, 12:13 PM   #6
wpeckham
Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix
Posts: 751

Rep: Reputation: 170Reputation: 170
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: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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,011

Rep: Reputation: 110Reputation: 110
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,376

Original Poster
Rep: Reputation: 147Reputation: 147
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
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration