Capitalize the first letter of a variable only: Kshell

Capitalize the first letter of a variable only: Kshell

Post by Teebu Phili » Sun, 06 May 2001 03:52:09



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:



fwdBldCall

fwdBldCAll

What am I doing wrong ?

Thanks in advance!

 
 
 

Capitalize the first letter of a variable only: Kshell

Post by Benjamin.Altma » Sun, 06 May 2001 04:40:47


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.


> 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:



> fwdBldCall

> fwdBldCAll

> What am I doing wrong ?

> Thanks in advance!


 
 
 

Capitalize the first letter of a variable only: Kshell

Post by Michael Velte » Sun, 06 May 2001 04:52:18



Quote:> 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

 
 
 

Capitalize the first letter of a variable only: Kshell

Post by Dan Merc » Sun, 06 May 2001 04:36:20




> 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:



> fwdBldCall

> fwdBldCAll

> What am I doing wrong ?

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

Quote:

> Thanks in advance!

--
Dan Mercer

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

 
 
 

Capitalize the first letter of a variable only: Kshell

Post by Teebu Phili » Sun, 06 May 2001 05:01:57


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


FWDBWDIND


> Date: Fri, 4 MAY 2001 21:52:18 +0200

> Newgroups: comp.unix.shell
> Subject: Re: Capitalize the first letter of a variable only: Kshell


> > 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

 
 
 

Capitalize the first letter of a variable only: Kshell

Post by Michael Velte » Sun, 06 May 2001 05:23:01



Quote:> 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