Hi,
Is there any function(Bash) out there that can convert the first character of each word to upper case?
Similar to php function Eg :
$foo = "hello world!";
$foo = ucwords($foo); //Hello Word
Manage to Google out this example :
first=`echo "hello world place"|nawk '{print substr($1,1,1)}'|tr '[:lower:]' '[:upper:]'`
however it only will display "Hello". Rest of the word are truncated. May I know what went wrong in the coding?
Thank you for the help in advance.
#!/bin/bash
foo="hello world place"
awk '{for(i=0;++i<=NF;){OFS=(i==NF)?RS:FS;printf toupper(substr($i,0,1)) substr($i,2) OFS }}' <<< "$foo"
Hello World Place
Without using any external commands:
$ _capwords the quick brown fox
$ printf "%s\n" "$_CAPWORDS"
The Quick Brown Fox
I have these functions in a library:
_upr()
{
_UPR=
case $1 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
}
_cap()
{
_upr "$1"
_CAP=$_UPR${1#?}
}
_capwords()
{
unset _CAPWORDS
set -f
for word in $*
do
_cap "$word"
_CAPWORDS="$_CAPWORDS${_CAPWORDS:+ }$_CAP"
done
}
foo="hello world place"
echo $foo | tr " " "\n" | nawk ' { out = out" "toupper(substr($0,1,1))substr($0,2) } END{ print substr(out,2) } '
Hello World Place