Lower casing letters of words

L

laredotornado

Hello,
Is there a simple perl command line expression I can write that will
take

"CORDOVA SCHOOL DISTRICT"

and make it

"Cordova School District"

The rule I'm using is that I'm lower casing every letter of that is
not the first of each word.


Ok, if that question was easy to answer, I have a slightly harder one.
Is there an expression I can write that will apply the above logic to a
certain column of a CSV file? Let's say I want to apply that to the
4th column of a file with a line like

,,"00000749","CORDOVA SCHOOL DISTRICT","PO BOX
140","CORDOVA","AK","99574","0140","907","424","3265","100 FISHERMAN
AVENUE","CORDOVA",
"AK","99574",510.00,36.43,-1,12,,"05","00000749","CORDOVA SCHOOL
DISTRICT",,"AKCORDOVA"

Any ideas? Thanks, - Dave
 
P

Paul Lalli

Hello,
Is there a simple perl command line expression I can write that will
take

"CORDOVA SCHOOL DISTRICT"

and make it

"Cordova School District"

s/(\w+)/\u\L$1/g;

Search for a word, and save it. Replace with that word, after it has
been transformed to all lowercases and had its first letter transformed
to uppercase.

The rule I'm using is that I'm lower casing every letter of that is
not the first of each word.


Ok, if that question was easy to answer, I have a slightly harder one.
Is there an expression I can write that will apply the above logic to a
certain column of a CSV file? Let's say I want to apply that to the
4th column of a file with a line like

,,"00000749","CORDOVA SCHOOL DISTRICT","PO BOX
140","CORDOVA","AK","99574","0140","907","424","3265","100 FISHERMAN
AVENUE","CORDOVA",
"AK","99574",510.00,36.43,-1,12,,"05","00000749","CORDOVA SCHOOL
DISTRICT",,"AKCORDOVA"

Any ideas? Thanks, - Dave

There are several CSV parsers and maniuplators available. Take a look
at Text::CSV for starters.

Paul Lalli
 
P

Paul Lalli

Paul said:
s/(\w+)/\u\L$1/g;

Bah. Should have checked the Perl FAQ before posting. Of course, so
should the OP...

perldoc -q capitalize
shows that this solution is sub-optimal at best, and recommends a
slightly longer alternative. I recommend you use the one suggested
there.
 
L

laredotornado

Hello, Thanks for your response. Was this meant to go in a script? I
created a script

#!/bin/ksh

while read -C -a array; do
array[3]=`echo ${array[2]} | perl -pi -e 's/(\w+)/\u\L$1/g'
myfile.txt`
array[4]=`echo ${array[4]} | perl -pi -e 's/(\w+)/\u\L$1/g'
myfile.txt`
array[5]=`echo ${array[5]} | perl -pi -e 's/(\w+)/\u\L$1/g'
myfile.txt`
echo "${array[*]|,,}"
done

Then I ran

../temp.sh

but got the error

../temp.sh[3]: read: bad option(s)

What am I doing incorrectly? Thanks, - Dave
 
R

RedGrittyBrick

Top posting corrected - please don't top-post.

Paul said:
Bah. Should have checked the Perl FAQ before posting. Of course,
so should the OP...

perldoc -q capitalize shows that this solution is sub-optimal at
best, and recommends a slightly longer alternative. I recommend
you use the one suggested there.

Hello, Thanks for your response. Was this meant to go in a script?
I created a script

#!/bin/ksh

while read -C -a array; do
array[3]=`echo ${array[2]} \
| perl -pi -e 's/(\w+)/\u\L$1/g' myfile.txt`
array[4]=`echo ${array[4]} \
| perl -pi -e 's/(\w+)/\u\L$1/g' myfile.txt`
array[5]=`echo ${array[5]} \
| perl -pi -e 's/(\w+)/\u\L$1/g' myfile.txt`
echo "${array[*]|,,}"
done

Then I ran
./temp.sh
but got the error
./temp.sh[3]: read: bad option(s)

What am I doing incorrectly?

That's a Korn shell error not a Perl error. Try `man ksh`. This
newsgroup (comp.lang.perl.misc) is not a good place to ask questions
about Korn shell. I don't have Korn shell to hand but you can probably
do what you want purely using Korn shell without resorting to Perl.

OTOH you can probably solve your problem purely in Perl without
resorting to ksh. I'd do that. YMMV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top