Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Capitalize the first letter of a variable only: Kshell

83 views
Skip to first unread message

Teebu Philip

unread,
May 4, 2001, 2:52:09 PM5/4/01
to
how in the world do i do this ?

i have tried using sed with "y" as follows:

fname="fwdBldCall"
nfw=`echo $fname | sed "y/[a-z]/[A-Z]/"`
echo "fname = $fname"
echo "nfw = $nfw"

this is what i get:
philip@thunder 267% fname="fwdBldCall"
philip@thunder 268% nfw=`echo $fname | sed "y/[a-z]/[A-Z]/"`
philip@thunder 269% echo $fname
fwdBldCall
philip@thunder 270% echo $nfw
fwdBldCAll


What am I doing wrong ?

Thanks in advance!


Benjamin.Altman

unread,
May 4, 2001, 3:40:47 PM5/4/01
to
sed doesn't work like tr - you need to explicitely state the entire
alphabet in both instances. but that won't help you change only the
first character. The way you have it set out, the entire variable will
be converted. You need:
nfw=`echo $fname | sed '/./
y/qwertyuiopasdfghjklzxcvbnm/QWERTYUIOPASDFGHJKLZXCVBNM/'

Notice the /./ at the beginning of the sed. In Korn shell you could do
this:
typeset -uL1 c=$fname
nfw=$c${fname#?}
which would be faster than sed.

Michael Velten

unread,
May 4, 2001, 3:52:18 PM5/4/01
to
* Teebu Philip <t...@duke.edu> in cus:

> i have tried using sed with "y" as follows:
> fname="fwdBldCall"
> nfw=`echo $fname | sed "y/[a-z]/[A-Z]/"`

»sed« is overkill here. What about just using »tr«?

nfw=`echo $fname | tr a-z A-Z`

or

nfw=`echo $fname | tr [:lower:] [:upper:]`

Michael

Dan Mercer

unread,
May 4, 2001, 3:36:20 PM5/4/01
to
In article <Pine.SOL.3.91.101050...@godzilla6.acpub.duke.edu>,

You are using sed unnecessarily and incorrectly. I'll leave it to a
seder to correct your sed usage, but capitalizing the first char
of a variable is easy in ksh.

$ x=abcdef
$ typeset -uL1 f=$x
$ print "${f}${x#?}"
Abcdef

the typeset command creates a special variable that is uppercase,
Left aligned and one char long. ${vname#pattern} trims the first
occurrance of pattern from the start of $vname. '?' matches a single
character.

${x#?} trims the first char from $x and returns the resulting string
>
> Thanks in advance!
>
>

--
Dan Mercer
dame...@mmm.com

Opinions expressed herein are my own and may not represent those of my employer.

Teebu Philip

unread,
May 4, 2001, 4:01:57 PM5/4/01
to

tr capitilizes all the letters ... i just want the first one to
be capitilized

philip@thunder 383% echo $fname | tr a-z A-Z
FWDBWDIND


On Fri, 4 May 2001, Michael Velten wrote:

> Date: Fri, 4 MAY 2001 21:52:18 +0200
> From: Michael Velten <usene...@michnet.de>
> Newgroups: comp.unix.shell
> Subject: Re: Capitalize the first letter of a variable only: Kshell

Michael Velten

unread,
May 4, 2001, 4:23:01 PM5/4/01
to
* Teebu Philip <t...@duke.edu> in cus:

> tr capitilizes all the letters ... i just want the first one to
> be capitilized

Oops, sorry. I should better go to bed. ;)
Forget my last posting and use the other internal ksh suggestions.

Michael

0 new messages