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

Change case of specific strings (columns) 1

Status
Not open for further replies.

gmgenova

Technical User
Mar 13, 2015
9
US
Hi. I have a file where the first name and last name are in columns 2 and 3. Currently, they are both in all caps. I want to change it so just the first letter of the first name and the first letter of the last name are in caps. I have some code below but I need it modified to only include string 2 and string 3 of my file. I am not sure how to do this. Thanks.

awk '{for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }'
 
Hi

If you only want to modify fields 2 and 3, then why are you iterating from 1 to NF ? [upsidedown]
Code:
awk '[teal]{[/teal][b]for[/b] [teal]([/teal][navy]i[/navy][teal]=[/teal][highlight][purple]2[/purple][/highlight][teal];[/teal] i[teal]<=[/teal][highlight][purple]3[/purple][/highlight][teal]; ++[/teal]i[teal]) {[/teal] [navy]$i[/navy][teal]=[/teal][b]toupper[/b][teal]([/teal][b]substr[/b][teal]([/teal][navy]$i[/navy][teal],[/teal][purple]1[/purple][teal],[/teal][purple]1[/purple][teal]))[/teal] [b]tolower[/b][teal]([/teal][b]substr[/b][teal]([/teal][navy]$i[/navy][teal],[/teal][purple]2[/purple][teal])); }[/teal] [b]print[/b] [teal]}[/teal]'

BTW, have you thought about handling multiple names and tricky orthography ?
[ul]
[li]Jean-[highlight]C[/highlight]laude Van [highlight]D[/highlight]amme[/li]
[li]Leonardo Di[highlight]C[/highlight]aprio[/li]
[li]Leonardo [highlight]d[/highlight]a [highlight]V[/highlight]inci[/li]
[/ul]


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top