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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP CHANGING CASE WITH AWK 1

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have the following awk script which I am using to print selected fields of the /etc/passwd file. I would like to change the case of the first letter of the user's first and last name (and the middle name if it exists) from lowercase to uppercase.

BEGIN {printf ("%-15s %-20s %-24s %-20s\n", "\nUsername"
,"Clock Number"," Name","Home Dir");
print "===========================================================================";FS=":|#|,"; OFS = "\t";}
{printf ("%-15s %-20s %-20s %-20s\n", $1, $5, $6, $10)}

The output currently displays:

Username Clock Number Name Home Dir

==========================================================
jim4 108556 jim bob /users/jim4
joe1 103546 joe b schmoe /users/j1


I would like it to display like:

Username Clock Number Name Home Dir

==========================================================
jim4 108556 Jim Bob /users/jim4
joe1 103546 Joe B Schmoe /users/j1

Any help would be greatly appreciated.

Thanks,

John
 
Could you please in future not SHOUT every time you post a new topic?

Add these functions to your program
Code:
function capitalise( word,
  n, arr, i, result ) {
  n = split( word, arr, " " )
  for ( i = 1 ; i <= n ; i++ ) {
    result = result &quot; &quot; cap(arr[i])
  }
  return result
}

function cap( word,
   a, b ) {
  a=substr(word,1,1)
  b=substr(word,2)
  return toupper(a) b
}

Then you can print
Code:
{printf (&quot;%-15s  %-20s %-20s %-20s\n&quot;, $1, $5, capitalise($6), $10)}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top