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