Lower casing letters of words

Discussion in 'Perl Misc' started by laredotornado@zipmail.com, Jul 15, 2005.

  1. Guest

    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
     
    , Jul 15, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > 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
     
    Paul Lalli, Jul 15, 2005
    #2
    1. Advertising

  3. Paul Lalli Guest

    Paul Lalli wrote:
    > wrote:
    > > 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;


    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.

    > Paul Lalli
     
    Paul Lalli, Jul 15, 2005
    #3
  4. Guest

    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

    Paul Lalli wrote:
    > Paul Lalli wrote:
    > > wrote:
    > > > 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;

    >
    > 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.
    >
    > > Paul Lalli
     
    , Jul 18, 2005
    #4
  5. Top posting corrected - please don't top-post.

    wrote:
    > Paul Lalli wrote:
    >
    >> Paul Lalli wrote:
    >>
    >>> wrote:
    >>>
    >>>> 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;

    >>
    >> 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.
     
    RedGrittyBrick, Jul 18, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kaeli
    Replies:
    8
    Views:
    600
    Chris Smith
    Nov 18, 2004
  2. Merrigan
    Replies:
    4
    Views:
    583
    Chris
    Dec 14, 2007
  3. timm
    Replies:
    1
    Views:
    162
  4. Venugopal
    Replies:
    11
    Views:
    1,567
    Tassilo v. Parseval
    Nov 5, 2003
  5. Omicron
    Replies:
    2
    Views:
    159
    Omicron
    Feb 10, 2008
Loading...

Share This Page