For Programmers: Free Programming Magazines  


Home > Archive > Unix Shell Programming > February 2005 > convert to uppercase or lowercase









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author convert to uppercase or lowercase
xavier

2005-02-09, 8:59 pm

Hi,

I'm writing a bash script and I need to convert strings to uppercase and
lowercase.
How can I do this ?

thanks

Xavier
Stachu 'Dozzie' K.

2005-02-09, 8:59 pm

On 2005-02-09, xavier wrote:
> Hi,
>
> I'm writing a bash script and I need to convert strings to uppercase and
> lowercase.
> How can I do this ?


Use tr: man tr

--
Stanislaw Klekot
Chris F.A. Johnson

2005-02-09, 8:59 pm

On Wed, 09 Feb 2005 at 19:27 GMT, xavier wrote:
> Hi,
>
> I'm writing a bash script and I need to convert strings to uppercase and
> lowercase.
> How can I do this ?


For long strings:

printf "%s\n" "$string" | tr '[a-z]' '[A-Z]'


For a short string (30 characters or less; YMMV):

_ucase() {
lower=thequickbrownfxjmpsvlazydg
upper=THEQUICKBROWNFXJMPSVLAZYDG
case $1 in
[a-z]) idx=${lower%$1*}
_UCASE=${upper:${#idx}:1}
;;
*) _UCASE=$1 ;;
esac
}

uword()
{
n=0
_UPPER=
word=$1
while [ "${word}" ]
do
_ucase ${word:$n:1}
_UPPER=$_UPPER$_UCASE
word=${word#?}
done
printf "%s\n" "$_UPPER"
}

uword "$string"


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Heiner Steven

2005-02-14, 8:58 pm

xavier wrote:

> I'm writing a bash script and I need to convert strings to uppercase and
> lowercase.
> How can I do this ?


Since others already pointed out a portable "tr" solution, here
is one for KornShell only:

$ typeset -u var=testing
$ echo "$var"
TESTING

Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner.steven@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
Christian Schneider

2005-02-14, 8:58 pm

Thus spake Heiner Steven (heiner.steven@nexgo.de):
> xavier wrote:
>
>
> Since others already pointed out a portable "tr" solution, here
> is one for KornShell only:
>
> $ typeset -u var=testing
> $ echo "$var"
> TESTING


Zsh only:
| $ var=testing
| $ echo $var:u

*scnr*
William Park

2005-02-15, 4:01 am

Christian Schneider <strcat@gmx.net> wrote:
> Thus spake Heiner Steven (heiner.steven@nexgo.de):
>
> Zsh only:
> | $ var=testing
> | $ echo $var:u


I can't resist... a patched Bash only, :-)

var=testing
set -- `tonumber $var` # 116 101 115 116 105 110 103
set -- `chnumber toupper $*` # 84 69 83 84 73 78 71
tostring $*

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
Slackware Linux -- because I can type.
Stan Milam

2005-02-16, 4:04 am

William Park wrote:
> Christian Schneider <strcat@gmx.net> wrote:
>
>
>
> I can't resist... a patched Bash only, :-)
>
> var=testing
> set -- `tonumber $var` # 116 101 115 116 105 110 103
> set -- `chnumber toupper $*` # 84 69 83 84 73 78 71
> tostring $*
>


Well, if it does not have the patch it is not worth having. I have been
reluctant to move to Linux because there is no Korn Shell.
Chris F.A. Johnson

2005-02-16, 4:04 am

On Wed, 16 Feb 2005 at 02:14 GMT, Stan Milam wrote:
>
> Well, if it does not have the patch it is not worth having. I have been
> reluctant to move to Linux because there is no Korn Shell.


Linux does have the Korn shell; there are two versions, pdksh which
is normally installed, and ksh93 which can be downloaded from
kornshell.com.

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Michael Wang

2005-02-16, 4:04 am

In article <y0yQd.47397$iC4.35581@newssvr30.news.prodigy.com>,
Stan Milam <stmilam@swbell.net> wrote:
>Well, if it does not have the patch it is not worth having. I have been
>reluctant to move to Linux because there is no Korn Shell.


A copy of ksh rpm package can be found from:

http://www.unixlabplus.com/linux-rpm/
--
For low fair air travel, take Independence Air - http://www.flyi.com/.
Michael Wang * http://www.unixlabplus.com/ * mwang@unixlabplus.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com