Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Changing the first word to uppercase

by Anonymous Monk
on Feb 14, 2008 at 00:04 UTC ( [id://667849]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello guys... i need a help badly, im dealing wid a simple problem for more than 12 hrs, u might laugh but i really peased off.... I have a dictinary of 5k words. each line one word n wid one space the meaning. i wanna chnage only n only the first word of each line to uppercase. i know how to change all, i know how to change field by field but i dont know this task as its not seprated in diff fields. the whole text is one field as below:
data hello h e l l o bye b y e bike b i k e . . .
plz suggest me a way... tx alot

Replies are listed 'Best First'.
Re: Changing the first word to uppercase
by ikegami (Patriarch) on Feb 14, 2008 at 00:28 UTC
    perl -pe"s/(.)/uc($1)/e" infile > outfile
    or
    while (<>) { s/(.)/uc($1)/e; print; }

    Update: Oops, switch lc with uc.

      What's the reason you chose s/.../uc($1)/e over s/.../\U$1/?
        I only thought of \U afterwards. I'm not a big fan of it either. Code shouldn't look like a string literal. And unlike interpolation, it's not something we are trained to look for.
      tx for ur help but it didnt work, it gave me the same line wid space after each line, it means my "word w o r d" now is "word w o r d word w o r d".....

        I accidentally used lc where I should have used uc, but you shouldn't get the behaviour you describe either way.

        >type infile hello h e l l o bye b y e bike b i k e >perl -pe"s/(.)/uc($1)/e" infile > outfile >type outfile Hello h e l l o Bye b y e Bike b i k e

        or

        >type infile hello h e l l o bye b y e bike b i k e >type script.pl while (<>) { s/(.)/uc($1)/e; print; } >perl script.pl infile > outfile >type outfile Hello h e l l o Bye b y e Bike b i k e

        You must be doing something extra.

      Dear Friend I still couldnt solve it:) it gives me an unexpected output:(

        We can't help solve "it doesn't work". Show us this code, the input you use, the output you get and the output you want.

        Update: I see you posted the input and output already, but not the code you used.

Re: Changing the first word to uppercase
by hipowls (Curate) on Feb 14, 2008 at 00:33 UTC

    You need to capture just the first word and replace it with uc $1. To make that work use the /e qualifier to the substitution which evaluates the replace ment as perl code.

    my $words ='one two three'; $words =~ s/^([[:alpha:]]+)/ uc $1 /e; print $words; __END__ ONE two three

      thanks but is there a way to use linux command line wid input n output file to make it?
        A little exercise using a sequence of bash shell commands (I'm including the "$" shell prompt, to make it clear which lines are actual commands; the lines without "$" are either keyboard input or command output):
        $ cat <<EOF > test.in one o n e two t w o three t h r e e EOF $ perl -pe 's/(\w+)/\U$1/' < test.in > test.out $ cat test.out ONE o n e TWO t w o THREE t h r e e
        If you want to store the one-line perl script as an executable file (so that you don't have to type  perl -pe 's/(\w+)/\U$1/' every time you want to up-case the first word of every line in a data stream), you would do this:
        $ cat <<EOF > upcase-firstword #!/usr/bin/perl -p s/(\w+)/\U$1/; EOF $ chmod +x upcase-firstword $ upcase-firstword < test.in > test2.out $ diff test.out test2.out
        Move the "upcase-firstword" file to some directory in your PATH, and you can run it no matter which directory you are in at the moment. (You can give it a shorter name if you like.)

        Using the "cat" command to write a perl script like that can be lots of fun, although many programmers would not consider it to be their preferred method for programming.

        If you want to change every line in some files then

        perl -pe's/^([[:alpha:]]+)/ uc $1 /e' -i.bak <file list>
        you can use a condition to change just some lines
        perl -pe's/^([[:alpha:]]+)/ uc $1 /e if condition' -i.bak <file list>
        The original file is saved with a extention of .bak

        The arguments from the command line are passed in via the @ARGV array. You can either write your own code to handle the args or parse them w/ Getopt::Std

        And by the way, since you're doing this from Linux -- if there's no particular reason you need to do this in Perl, you can check into the utility sed, which was written to do this sort of thing, and will save you some typing.
Re: Changing the first word to uppercase
by johngg (Canon) on Feb 14, 2008 at 10:12 UTC
    i know how to change field by field but i dont know this task as its not seprated in diff fields. the whole text is one field as below

    I'm not sure what you mean by this. Surely the line has separate fields delimited by space characters. Uppercasing the first field seems to work although other proposed solutions using regex substitution are the best way to go.

    use strict; use warnings; print map { join q{ }, uc $_->[ 0 ], @$_[ 1 .. $#{ $_ } ] } map { [ split m{ } ] } <DATA>; __END__ hello h e l l o bye b y e bike b i k e

    Produces

    HELLO h e l l o BYE b y e BIKE b i k e

    I hope this is useful.

    Cheers,

    JohnGG

Re: Changing the first word to uppercase
by MidLifeXis (Monsignor) on Feb 14, 2008 at 16:11 UTC

    This is a bit late, but is this, by chance, homework?

    <Update>: (current XP == -1) Not sure what the problem is with my node:

    • abbreviated writing pattern - more prevelent in the HS / College / IM crowd. If due to language barrier - my apologies up front.
    • No code.
    • large block of time working on a problem with an "obvious" perl solution (no offense indended to OP, but this is the stuff that perl does very well and simply).
    • AM - OP is not a monk with a history.
    • Class is is session
    • The problem sounds very much in the vein of a question from my introductory programming courses.

    So, what is the issue. If this sounds snarkey, tough. I do my work, and like to help others when they show an effort. No effort == no desire to provide help.

    </Update>

    --MidLifeXis

Re: Changing the first word to uppercase
by takshaka (Friar) on Feb 14, 2008 at 17:57 UTC

    I must be missing something. Why all the regexes?

    perl -pe "$_=ucfirst" infile > outfile

    Update: Ah, first word, not first letter. Time for another five-year hiatus, I guess, as my reading comprehension has atrophied.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://667849]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found