I need to use bash to convert sentences where all words start with a small letter into one where all words start with a capital letter.
So that a string like:
are utilities ready for hurricane sandy
becomes:
Are Utilities Ready For Hurricane Sandy
Had this sitting around. Use nawk on solaris.
awk 'function titlecase(STR, N, M, A, OUT)
{
OUT=""
N=split(STR, A, " ");
for(M=1; M<=N; M++)
{
A[M]=tolower(A[M]);
A[M]=toupper(substr(A[M],1,1)) substr(A[M], 2);
OUT=OUT" "A[M]
}
return(substr(OUT, 2));
}
{ $0=titlecase($0) } 1' inputfile > outputfile
1 Like
Yoda
January 7, 2013, 5:48pm
3
In BASH using tr
command:
#!/bin/bash
for words in are utilities ready for hurricane sandy
do
for((i=0;i<${#words};i++))
do
if [ $i -eq 0 ]
then
echo -e "${words:$i:1}\c" | tr '[a-z]' '[A-Z]'
else
echo -e "${words:$i:1}\c"
fi
done
echo -e " \c"
done
echo -e "\n"
1 Like
rdrtx1
January 7, 2013, 6:02pm
4
try also:
typeset -u fl
while read -a line
do
c=0
for word in ${line[@]}
do
fl=${word[@]:0:1}
line[$c]="$fl${word[@]:1}"
(( c = c + 1 ))
done
echo ${line[@]}
done < infile
or with awk (to preserve blank space format):
awk '{for (i=1; i<=NF; i++) {$i=(toupper(substr($i,1,1)) substr($i,2))}} 1' infile
1 Like
binlib
January 7, 2013, 8:36pm
5
bash$ set -- are utilities ready for hurricane sandy
bash$ echo "${@^}"
Are Utilities Ready For Hurricane Sandy
3 Likes
if you have GNU sed, then try the below
$ echo "one two three Four" | sed 's,^.,\U&,;s, .,\U&,g'
One Two Three Four
That is a very neat trick!
Yoda
January 8, 2013, 12:34pm
8
I tried binlib's approach on GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu) but it didn't work. Got below error:
${@^}: bad substitution
Which version does this need?
---------- Post updated at 11:34 ---------- Previous update was at 11:29 ----------
Ok, got it. Looks like it works only on Bash version 4
I tried binlib's approach on GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu) but it didn't work. Got below error:
${@^}: bad substitution
Which version does this need?
---------- Post updated at 11:34 ---------- Previous update was at 11:29 ----------
Ok, got it. Looks like it works only on Bash version 4
Yes,
these case modification parameter expansion require bash >= 4.
With zsh:
zsh-5.0.0% s='are utilities ready for hurricane sandy'
zsh-5.0.0% print -- "${(C)s}"
Are Utilities Ready For Hurricane Sandy
... and Perl:
perl -le'print join " ", map ucfirst, split /\s+/, shift' 'are utilities ready for hurricane sandy'
Are Utilities Ready For Hurricane Sandy
3 Likes
binlib
January 8, 2013, 4:10pm
10
Nice solutions, although the perl solution can be shortened:
perl -le'$_=shift;s/\b(.)/\u$1/g;print' 'are utilities ready for hurricane sandy'
or
perl -ple's/\b./\u$&/g' <<<'are utilities ready for hurricane sandy'
1 Like
binlib:
Nice solutions, although the perl solution can be shortened:
perl -le'$_=shift;s/\b(.)/\u$1/g;print' 'are utilities ready for hurricane sandy'
or
perl -ple's/\b./\u$&/g' <<<'are utilities ready for hurricane sandy'
Yes,
just removing the l switch
zsh-5.0.0% perl -pes/'\b./\u$&'/g<<<'are utilities ready for hurricane sandy'
Are Utilities Ready For Hurricane Sandy