Unix Shell - Re: convert 1st char of a word to uppercase

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2005 > Re: convert 1st char of a word to uppercase





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 Re: convert 1st char of a word to uppercase
Icarus Sparry

2005-04-25, 2:52 am

On Mon, 25 Apr 2005 04:03:10 +0000, Hans Horn wrote:

> Group,
> is there a simple way to convert just the first char of a word to uppercase?


It depends on what you mean by 'simple', where the 'word' is, and what you
mean by 'uppercase'. If we restrict ourselves to ASCII, and have the word as
the first characters in a variable, then we can do something like this.

RESTWORD=${WORD/?/}
FIRSTCHAR=${WORD%${RESTWORD}}
case "$FIRSTCHAR" in
a) FIRSTCHAR="A";;
b) FIRSTCHAR="B";;
....
z) FIRSTCHAR="Z";;
esac

NEWWORD="${FIRSTCHAR}${RESTWORD}"

This uses modern shell builtins (ksh93, bash3), to be fast. Otherwise you can
use sed, e.g.

NEWWORD=$(echo "$WORD" | sed 'h;s/\(.\).*/\1/;
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G;s/\n.//' )

which says save the input
change the input into just the first character
change the input into the upper case version
join the original input onto the upper case first letter
remove the join character and the next character (the first of the original)

If the word is in a file, then 'ex' and 'emacs' have facilities to convert
to and from upper case.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com