Unix Shell - first letter of the word in CAPS

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2006 > first letter of the word in CAPS





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 first letter of the word in CAPS
aarcee

2006-08-22, 1:38 am

Hello all ,

I need to display a word with the first character in CAPS .

that is , split then the display should be Split

Thanks
RC

Hubble

2006-08-22, 7:31 am


aarcee schrieb:

> Hello all ,
>
> I need to display a word with the first character in CAPS .
>
> that is , split then the display should be Split
>
> Thanks
> RC


#!/bin/sh
PATH=/bin:/usr/bin

for arg
do
firstchar=`echo "$arg" | cut -c1`
# assume echo -n does not print newline
echo -n $firstchar | tr a-z A-Z
echo "$arg" | sed -s s/^.//
done


Hubble

Chris F.A. Johnson

2006-08-22, 7:31 am

On 2006-08-22, aarcee wrote:
> Hello all ,
>
> I need to display a word with the first character in CAPS .


You mean CAP, not CAPS, if it is only one character.

If you have the word in $word:

case $word in
a*) _UPR=A ;; b*) _UPR=B ;;
c*) _UPR=C ;; d*) _UPR=D ;;
e*) _UPR=E ;; f*) _UPR=F ;;
g*) _UPR=G ;; h*) _UPR=H ;;
i*) _UPR=I ;; j*) _UPR=J ;;
k*) _UPR=K ;; l*) _UPR=L ;;
m*) _UPR=M ;; n*) _UPR=N ;;
o*) _UPR=O ;; p*) _UPR=P ;;
q*) _UPR=Q ;; r*) _UPR=R ;;
s*) _UPR=S ;; t*) _UPR=T ;;
u*) _UPR=U ;; v*) _UPR=V ;;
w*) _UPR=W ;; x*) _UPR=X ;;
y*) _UPR=Y ;; z*) _UPR=Z ;;
*) _UPR=${1%${1#?}} ;;
esac
word=$_UPR${word#?}

printf "s\n" "$word"

> that is , split then the display should be Split


What display? Split how? Why?

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Stephane Chazelas

2006-08-22, 7:31 am

On 21 Aug 2006 23:33:58 -0700, aarcee wrote:
> Hello all ,
>
> I need to display a word with the first character in CAPS .
>
> that is , split then the display should be Split

[...]


With zsh:

word=foo
Word=${(C)word}

(will captitalise every word in $word (turns " foo bar" into
" Foo Bar").

With other shells:

Word=$(
awk '
BEGIN {
print toupper(substr(ARGV[1], 1, 1)) \
tolower(substr(ARGV[1], 2))
exit
}' "$word"
)

Captitalises only the first character in $word.

--
Stephane
William Park

2006-08-22, 7:34 pm

aarcee <4mystudies@gmail.com> wrote:
> Hello all ,
>
> I need to display a word with the first character in CAPS .
>
> that is , split then the display should be Split


a='split'
echo ${a|.capitalize}

Ref:
http://home.eol.ca/~parkw/index.htm...meter_expansion

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Xicheng Jia

2006-08-22, 7:34 pm

aarcee wrote:
> Hello all ,
>
> I need to display a word with the first character in CAPS .
>
> that is , split then the display should be Split


GNU sed:

bash:~$ echo 'split' | sed 's/./\u&/'
Split
bash:~$ echo 'cygwin split' | sed 's/\([^ ]\+\)/\u\1/g'
Cygwin Split

--
XC

mik3

2006-08-23, 1:26 pm


aarcee wrote:
> Hello all ,
>
> I need to display a word with the first character in CAPS .
>
> that is , split then the display should be Split
>
> Thanks
> RC


if you have Python in your unix version,[vbcol=seagreen]
'Split'[vbcol=seagreen]

aarcee

2006-08-24, 7:26 am

thanks a log for all the answers.
mik3 wrote:[vbcol=seagreen]
> aarcee wrote:
>
> if you have Python in your unix version,
> 'Split'

aarcee

2006-08-24, 7:26 am

thanks a lot for all the answers.
mik3 wrote:[vbcol=seagreen]
> aarcee wrote:
>
> if you have Python in your unix version,
> 'Split'

bsh

2006-08-24, 7:26 pm


aarcee wrote:
> I need to display a word with the first character in CAPS .


You haven't mentioned which shell.

=Brian

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com