Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ensure initial letter uppercase

Status
Not open for further replies.

risby

Programmer
Feb 27, 2002
84
I have some email addresses and I want to strip out the name. They are all similar format: forename dot surname at domain, and they are usually all lower case. I know it's very old fogeyish of me but I thought it would be better to have the first letter of the extracted names in uppercase. Below is what I tried (apart from the awk it's Korn shell syntax, ascii char set, ADM holds email address and NME should end up with the name component)
Code:
    NME=$( echo ${ADM} | cut -d'@' -f1 | tr '.' ' ' |
     awk '
     {
       for (i=1; i<=NF; i++) {
         initial=substr($i, 1, 1)
         if (initial > "Z") {
           initial += "a" - "A"
           $i=initial substr($i, 2)
         }
       }
       print $0
     }
     '
    )
but I get a zero instead of the uppercase initial I want, i.e. I get 0red 0loggs instead of Fred Bloggs.

It's some difference between awk and c handling of datatypes I guess but I can't see what the problem is.

==========================================
toff.jpg
Some cause happiness wherever they go; others whenever they go.
 
use toupper function

Code:
awk '{print toupper(substr($1,1,1)) substr($1,2)}'


HTH,

p5wizard
 
Of course, I forgot about that. Thx.

==========================================
toff.jpg
I phoned the local ramblers club today, and this bloke just went on and on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top