Click to See Complete Forum and Search --> : Capitals


Dabos
02-25-2003, 07:49 AM
I simply have a list of names in a file which can consist of either

christian name and surname
christian name, middle name and surname
christian name, initial and surname

The capitalisation is not fixed, ie their names may not start with capital letters and may have capital letters in the middle of their names. I need to output their names with capital letters at the beginning of each name/initial and little letters after this.

I'm not sure what utility to use. I have written most of my program using awk (this is only part of my program).

Any advice would be much appreciated.

joesbox
02-25-2003, 08:48 AM
this is an excerpt from the perl cookbook by O'Reily.

members.cox.net/joes_box/perlbook.html

hope this helps

Dabos
02-25-2003, 08:59 AM
Sorry to be annoying but I've done the res of my script using awk. I'd rather not use a perl file to do this. Are there any other methods?

Energon
02-26-2003, 10:16 AM
If awk does tr style regex, use that to change it all to lower-case, then change the first letter to upper-case. If it's a bash script, use the tr program to do it.

Dabos
02-26-2003, 12:07 PM
I know how to change the whole word to lower case, but then don't know how to specify changing the FIRST letter of each word to a capital.

Any suggestions?

joesbox
02-26-2003, 12:24 PM
i know that in perl you can substitute with a variable. is there any way that you can take the first letter off make it uppercase and then substitute the put it back on to the front of the word.

something like this.

$letter=word;
($firstletter)=$letter =~ /$\w/; #take the first letter found in the var $letter and put it in the var $firstletter
$letter=~ s/$\w//; #take the first letter off of the var $letter
$firstletter = uc($firstletter); #make the letter in $firstletter uppercase
$letter= $firstletter$letter; #now make $letter = the now capped $firstletter and $letter combined
print $letter; # print to make sure that it worked

btw this is perl, i don't know awk. i believe that this would work. i haven't tested it but it is an idea.

Dabos
02-26-2003, 12:59 PM
I know how to get tr to change the whole of the word to lower case, but I don't know how to then change the FIRST letter of every word to upper case. I don't know how to specify this first letter using tr or sed.

Any suggestions?

Dabos
02-26-2003, 01:00 PM
Opps sorry for posting that again. I can't use perl for this project. It doesn't have to be awk, it just can't be perl.

chrism01
02-26-2003, 01:43 PM
ok: here's shell soln:

tt=abc
tt2=`echo $tt|cut -c1` # gives 'a'
tt1=`echo ${#tt}` # gives 3 ie num of chars in tt

tt3=`echo $tt|cut -c1,$tt1` # gives 'bc'

then echo the var containing 'a' through tr eg
tt4=`echo $tt2|tr [a-z] A-Z]`

then glue bits back together

tt5=$tt4$tt3

:)

joesbox
02-26-2003, 04:15 PM
i wasn't meaning for you to use perl. i knew that it wasn't an option. i just had an idea and only knew how to express it using perl. i don't know any other languages except for javascript, html & perl.

iDxMan
02-26-2003, 08:45 PM
Why is perl prohibited? A school project?

-r

Energon
02-26-2003, 09:54 PM
This is the solution I figured, assuming you've already split each name part into its own var (not hard, I'm sure you've already done it):


for each name variable
make the variable lowercase
copy the variable to a temp variable
remove everything but the first letter from the temp var:
s/(\w)\w*/$1/
upper-case the temp var
combine them back into the name var:
name_var =~ s/\w(\w*)/temp_var$1/


I did it in Perl, and then took that into my english code. If you know and can do regex, it should work (you might try sed if awk can't do it).