in reply to Changing the first word to uppercase

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

Replies are listed 'Best First'.
Re^2: Changing the first word to uppercase
by Anonymous Monk on Feb 14, 2008 at 00:38 UTC
    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.
        I was working with "sed" but it didnt give me the right answer indeed or i couldnt figure it out well!!! thats why i could only find the perl way....