Operating System - HP-UX
1834346 Members
1851 Online
110066 Solutions
New Discussion

changing case of first letter in a string

 
SOLVED
Go to solution
Vasikaran Venkatesan
Frequent Advisor

changing case of first letter in a string

I need to convert the case of the first letter from lower to upper - tried few - but would like to get a more simpler way.
for eg
philly -> Philly

Any help ?
thanks.
5 REPLIES 5
Hein van den Heuvel
Honored Contributor
Solution

Re: changing case of first letter in a string

echo test | perl -p -e '$_ = ucfirst()'

Hein.
A. Clay Stephenson
Acclaimed Contributor

Re: changing case of first letter in a string

We can very easily leverage Perl's ucfirst function.

I'll do it mainly in Shell in case you don't want to use (much) Perl:

#!/usr/bin/sh

VARS="dog cat possum"
for VAR in ${VARS}
do
UVAR=$(echo "${VAR}" | perl -e 'while (<>) {print ucfirst $_};')
echo "${VAR} -> ${UVAR}"
done
If it ain't broke, I can fix that.
Kent Ostby
Honored Contributor

Re: changing case of first letter in a string

This will work for single words that you input.

So if you had a script called "script" then "script philly" would yield "Philly".

I'm tied up right now or I'd work on something that could process a whole file.

#!/bin/sh
myfirst=`echo $1 | cut -c1 | tr "[:lower:]" "[:upper:]"`
myrest=`echo $1 | cut -c2-128 `
echo $myfirst$myrest
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
john korterman
Honored Contributor

Re: changing case of first letter in a string

Hi,
the korn shell has some functions that can be used creatively - perhaps this is overdoing it:

# unset FUP
# FUP=fup
# END_F=${FUP#?}
# typeset -u BEG_F=${FUP%$END_F}
# echo "$BEG_F$END_F"
Fup

regards,
John K.
it would be nice if you always got a second chance
Elena Leontieva
Esteemed Contributor

Re: changing case of first letter in a string

The TC shell has an upper- and lowercase modifiers.

> set name = philly
> echo $name:u
> Philly

You can get it from:
http://hpux.cs.utah.edu/hppd/cgi-bin/search

Elena.