changing first letter to CAPS

Hi,

I want to convert the first letter of this word from lowecase to uppercase.

Assume a letter united. I want to translate to United

Please let me know a simple way to do that.

Thanks.

echo united | awk '{print toupper(substr($0,1,1))""substr($0,2)}'

Hi Shamrock,

Thanks for the reply. But this is not working.

agamemnon:/home/x033870>echo `hostname` | awk '{print toupper(substr($0,1,1))""substr($0,2)}'
agamemnon

echo `hostname` | awk 'BEGIN{OFS=FS=""}{$1=toupper($1);print}'

In HP UX it is converting everything

genova$ /Application/psoftlnk/hr83:echo `hostname` | awk 'BEGIN{OFS=FS=""}{$1=to
upper($1);print}'
GENOVA

In SUn OS nothing changed..its all lowercase...

i want only the first letter

Sort of cheating since it requires perl installed:

$ echo united | perl -pe 's/^(\w)/\u$1/'
United

Personally i feel that sed should be given a chance then we should think about awk and perl(perl commands are more or less same like sed)

echo united | sed 's/^./\U/g'

i hope this would work

Best Regards,
Rakesh Uv

That won't work. This will, though:

$ echo united | sed 's/^\(.\)/\U\1/'
United

First of all two things.

In your both sed commands you have given the letter itself..where i can simple use tr cmmand for that.

echo united|tr u U

but my requirement is different. I am going to echo the hostname here and do the change. I will be implementing in multiple hosts where i cant go ahead change the scripts in all the hosts. I want to do this without specifying any letters. The command should convert what ever is first letter.

echo `hostname`|??

Thanks.

@sandman

Perl command works pefectly. It meets my requirement.Anything similar to that using shell?

try this one

tmp=united

typeset -L1 -u fl
typeset -R$((${#tmp}-1)) rl

fl=$tmp
rl=$tmp
new=${fl}${rl}

\U means "convert to uppercase" and shouldn't be confused with the "u" in united :wink:

The sed command I provided will uppercase whatever letter you throw at it (at least us-ascii):

$ for airline in united delta american southwest; do
> echo $airline | sed 's/^\(.\)/\U\1/'
> done
United
Delta
American
Southwest

Also, for what it's worth, the following two commands give identical output:

$ hostname
localhost
$ echo `hostname`
localhost

So this will be the magic line you need

hostname | sed 's/^\(.\)/\U\1/'

Sandman thanks for your replies. Sandman i dont know what flavour of unix you are using. I am using HP-UX. below is my command output using sed.

genova$ /:hostname | sed 's/^\(.\)/\U\1/'
Ugenova

genova$ /:for airline in united delta american southwest; do
> echo $airline | sed 's/^\(.\)/\U\1/'
> done
Uunited
Udelta
Uamerican
Usouthwest
genova$ /:

even sunos does the same.

x033870@agamemnon:/home/x033870>hostname | sed 's/^\(.\)/\U\1/'
Uagamemnon
x033870@agamemnon:/home/x033870>uname -a
SunOS agamemnon 5.8 Generic_117350-45 sun4u sparc SUNW,Ultra-80
x033870@agamemnon:/home/x033870>

This is why i got confused with "U"

Here is a way to do it in ksh93 using a custom builtin

/*
 * firstcap.c
 *
 * ksh93 custom builtin (error checking limited!)
 *
 */

#include <stdio.h>
#include <ctype.h>

int
b_firstcap(int argc, char *argv[], void *context)
{
    int c;
    char *s;

    if (argc != 2) {
       fprintf(stderr,"Usage: firstcap arg\n");
       return(2);
    }

    s = argv[1];
    c = *s++;

    printf("%c%s\n", toupper(c), s);

    return(0);
}

Here is how to compile and use it within ksh93

$ gcc -c firstcap.c
$ gcc -shared -o firstcap.so firstcap.o -lc
$ builtin -f ./firstcap.so firstcap
$ firstcap finnbarr
Finnbarr
$ firstcap united
United
$

Krrishv, I was running this under Linux.
I didn't know the behavior of sed varied that much between different flavors, but I guess you're right:

I just tried my command on a FreeBSD box and it did not work. :mad:

It looks like \U is a GNU extension:
sed, a stream editor

Any other suggestions?

perl -pe 's/(.)/\U\1/'

I don't understand why shamrock's awk solution would not work, though. Are you sure?

And I don't uderstand either why the sed solution doesn't work. In addition, for words like anglo-american or o'brian try this:

echo 'anglo-american' | sed  's/\(^.\|[-\\''].\)/\U\1/g'

The \U thing is a relatively recent, non-standard extension to sed. (Actually it's relatively new in Perl, too; I recall it was only added in version 5.something.) This guy is on HP-UX, which I understand basically means they stopped adding features in 1979.

A ksh solution that requires no plugin...

$ cat up1
#! /usr/bin/ksh
typeset -L1 -u first
read word
first=$word
echo ${first}${word#?}
exit 0
$
$
$ echo hello | ./up1
Hello
$