Capitalize first lower-case letter on each line of a file - awk

This is a discussion on Capitalize first lower-case letter on each line of a file - awk ; In a shell script, I need to capitalize the first lower-case letter on each line of a file. The first lower-case letter on each line need not be the first character on the line, since it may be preceeded by ...

+ Reply to Thread
Results 1 to 10 of 10

Capitalize first lower-case letter on each line of a file

  1. Default Capitalize first lower-case letter on each line of a file

    In a shell script, I need to capitalize the first lower-case letter on
    each line of a file. The first lower-case letter on each line need not
    be the first character on the line, since it may be preceeded by
    digits, upper-case letters, white space, or punctuation. I prefer a
    solution using sed, but an awk solution (not oawk) is acceptable.
    Several sample input and output lines are shown below:

    abc => Abc
    1abc => 1Abc
    Abc => ABc
    ABC => ABC
    123 => 123

    I spent about an hour playing with this, and found only
    ugly-in-the-extreme solutions. Any ideas?

    Many thanks,

    Phil


  2. Default Re: Capitalize first lower-case letter on each line of a file

    pbewig@swbell.net wrote:
    > In a shell script, I need to capitalize the first lower-case letter on
    > each line of a file. The first lower-case letter on each line need not
    > be the first character on the line, since it may be preceeded by
    > digits, upper-case letters, white space, or punctuation. I prefer a
    > solution using sed, but an awk solution (not oawk) is acceptable.
    > Several sample input and output lines are shown below:
    >
    > abc => Abc
    > 1abc => 1Abc
    > Abc => ABc
    > ABC => ABC
    > 123 => 123
    >
    > I spent about an hour playing with this, and found only
    > ugly-in-the-extreme solutions. Any ideas?


    What's "ugly"?

    awk 'i=match($0,/[a-zA-Z]/){
    $0=substr($0,1,i-1) toupper(substr($0,i,1)) substr($0,i+1)}1'


    Janis

    >
    > Many thanks,
    >
    > Phil
    >


  3. Default Re: Capitalize first lower-case letter on each line of a file

    pbewig@swbell.net wrote:
    > In a shell script, I need to capitalize the first lower-case letter on
    > each line of a file. The first lower-case letter on each line need not
    > be the first character on the line, since it may be preceeded by
    > digits, upper-case letters, white space, or punctuation. I prefer a
    > solution using sed, but an awk solution (not oawk) is acceptable.
    > Several sample input and output lines are shown below:
    >
    > abc => Abc
    > 1abc => 1Abc
    > Abc => ABc
    > ABC => ABC
    > 123 => 123
    >
    > I spent about an hour playing with this, and found only
    > ugly-in-the-extreme solutions. Any ideas?
    >
    > Many thanks,
    >
    > Phil


    awk '{ match($0,/[a-z]/)
    if (RSTART)
    { c=substr($0,RSTART,1);sub(c,toupper(c))}
    print}' myfile


  4. Default Re: Capitalize first lower-case letter on each line of a file


    William James wrote:
    > pbewig@swbell.net wrote:
    > > In a shell script, I need to capitalize the first lower-case letter on
    > > each line of a file. The first lower-case letter on each line need not
    > > be the first character on the line, since it may be preceeded by
    > > digits, upper-case letters, white space, or punctuation. I prefer a
    > > solution using sed, but an awk solution (not oawk) is acceptable.
    > > Several sample input and output lines are shown below:
    > >
    > > abc => Abc
    > > 1abc => 1Abc
    > > Abc => ABc
    > > ABC => ABC
    > > 123 => 123
    > >
    > > I spent about an hour playing with this, and found only
    > > ugly-in-the-extreme solutions. Any ideas?
    > >
    > > Many thanks,
    > >
    > > Phil

    >
    > awk '{ match($0,/[a-z]/)
    > if (RSTART)
    > { c=substr($0,RSTART,1);sub(c,toupper(c))}
    > print}' myfile


    ruby -pe 'sub(/([a-z])/){$1.upcase}' myfile


  5. Default Re: Capitalize first lower-case letter on each line of a file

    I was thinking primarily in sed, because the rest of the shell script
    finds sed more convenient than awk, and forgot that gawk extends
    standard nawk with a toupper function. Dumb.

    Many thanks to all who responded.

    Phil

    pbewig@swbell.net wrote:
    > In a shell script, I need to capitalize the first lower-case letter on
    > each line of a file. The first lower-case letter on each line need not
    > be the first character on the line, since it may be preceeded by
    > digits, upper-case letters, white space, or punctuation. I prefer a
    > solution using sed, but an awk solution (not oawk) is acceptable.
    > Several sample input and output lines are shown below:
    >
    > abc => Abc
    > 1abc => 1Abc
    > Abc => ABc
    > ABC => ABC
    > 123 => 123
    >
    > I spent about an hour playing with this, and found only
    > ugly-in-the-extreme solutions. Any ideas?
    >
    > Many thanks,
    >
    > Phil



  6. Default Re: Capitalize first lower-case letter on each line of a file

    pbewig@swbell.net wrote:
    >

    [please don't top-post. Fixed below]


    > pbewig@swbell.net wrote:
    >
    >>In a shell script, I need to capitalize the first lower-case letter on
    >>each line of a file. The first lower-case letter on each line need not
    >>be the first character on the line, since it may be preceeded by
    >>digits, upper-case letters, white space, or punctuation. I prefer a
    >>solution using sed, but an awk solution (not oawk) is acceptable.
    >>Several sample input and output lines are shown below:
    >>
    >>abc => Abc
    >>1abc => 1Abc
    >>Abc => ABc
    >>ABC => ABC
    >>123 => 123
    >>
    >>I spent about an hour playing with this, and found only
    >>ugly-in-the-extreme solutions. Any ideas?
    >>
    >>Many thanks,
    >>
    >>Phil

    >
    > I was thinking primarily in sed, because the rest of the shell script


    sub("the rest of the shell script","the author of the shell script") ;-)

    > finds sed more convenient than awk, and forgot that gawk extends
    > standard nawk with a toupper function. Dumb.


    nawk isn't standard but IIRC it does have toupper() as does any awk that
    conforms to the IEEE standard
    (http://www.opengroup.org/onlinepubs/...ities/awk.html).

    Ed.

  7. Default Re: Capitalize first lower-case letter on each line of a file

    pbewig@swbell.net wrote:
    > In a shell script, I need to capitalize the first lower-case letter on
    > each line of a file. The first lower-case letter on each line need not
    > be the first character on the line, since it may be preceeded by
    > digits, upper-case letters, white space, or punctuation. I prefer a
    > solution using sed, but an awk solution (not oawk) is acceptable.
    > Several sample input and output lines are shown below:
    >
    > abc => Abc
    > 1abc => 1Abc
    > Abc => ABc
    > ABC => ABC
    > 123 => 123
    >
    > I spent about an hour playing with this, and found only
    > ugly-in-the-extreme solutions. Any ideas?
    >
    > Many thanks,
    >
    > Phil
    >

    Know it is an awk newsgroup, but as you asked about sed,
    this would work in gnu sed.

    sed -e 's/[a-z]/\u&/'


  8. Default Re: Capitalize first lower-case letter on each line of a file

    The rest of the shell script is mostly a long series of 's/.../.../'
    commands, which are more convenient in sed than awk. That has nothing
    to do with the author of the script.

    The awk book doesn't mention toupper, which is why I always forget
    about it.

    Ed Morton wrote:
    > pbewig@swbell.net wrote:
    > >

    > [please don't top-post. Fixed below]
    >
    >
    > > pbewig@swbell.net wrote:
    > >
    > >>In a shell script, I need to capitalize the first lower-case letter on
    > >>each line of a file. The first lower-case letter on each line need not
    > >>be the first character on the line, since it may be preceeded by
    > >>digits, upper-case letters, white space, or punctuation. I prefer a
    > >>solution using sed, but an awk solution (not oawk) is acceptable.
    > >>Several sample input and output lines are shown below:
    > >>
    > >>abc => Abc
    > >>1abc => 1Abc
    > >>Abc => ABc
    > >>ABC => ABC
    > >>123 => 123
    > >>
    > >>I spent about an hour playing with this, and found only
    > >>ugly-in-the-extreme solutions. Any ideas?
    > >>
    > >>Many thanks,
    > >>
    > >>Phil

    > >
    > > I was thinking primarily in sed, because the rest of the shell script

    >
    > sub("the rest of the shell script","the author of the shell script") ;-)
    >
    > > finds sed more convenient than awk, and forgot that gawk extends
    > > standard nawk with a toupper function. Dumb.

    >
    > nawk isn't standard but IIRC it does have toupper() as does any awk that
    > conforms to the IEEE standard
    > (http://www.opengroup.org/onlinepubs/...ities/awk.html).
    >
    > Ed.



  9. Default Re: Capitalize first lower-case letter on each line of a file

    I didn't know about \u. Thanks.

    Jon LaBadie wrote:
    > pbewig@swbell.net wrote:
    > > In a shell script, I need to capitalize the first lower-case letter on
    > > each line of a file. The first lower-case letter on each line need not
    > > be the first character on the line, since it may be preceeded by
    > > digits, upper-case letters, white space, or punctuation. I prefer a
    > > solution using sed, but an awk solution (not oawk) is acceptable.
    > > Several sample input and output lines are shown below:
    > >
    > > abc => Abc
    > > 1abc => 1Abc
    > > Abc => ABc
    > > ABC => ABC
    > > 123 => 123
    > >
    > > I spent about an hour playing with this, and found only
    > > ugly-in-the-extreme solutions. Any ideas?
    > >
    > > Many thanks,
    > >
    > > Phil
    > >

    > Know it is an awk newsgroup, but as you asked about sed,
    > this would work in gnu sed.
    >
    > sed -e 's/[a-z]/\u&/'



  10. Default Re: Capitalize first lower-case letter on each line of a file

    pbewig@swbell.net wrote:

    [please don't top-post. Fixed below]

    > Ed Morton wrote:
    >
    >>pbewig@swbell.net wrote:
    >>
    >>[please don't top-post. Fixed below]
    >>
    >>
    >>
    >>>pbewig@swbell.net wrote:
    >>>
    >>>
    >>>>In a shell script, I need to capitalize the first lower-case letter on
    >>>>each line of a file. The first lower-case letter on each line need not
    >>>>be the first character on the line, since it may be preceeded by
    >>>>digits, upper-case letters, white space, or punctuation. I prefer a
    >>>>solution using sed, but an awk solution (not oawk) is acceptable.
    >>>>Several sample input and output lines are shown below:
    >>>>
    >>>>abc => Abc
    >>>>1abc => 1Abc
    >>>>Abc => ABc
    >>>>ABC => ABC
    >>>>123 => 123
    >>>>
    >>>>I spent about an hour playing with this, and found only
    >>>>ugly-in-the-extreme solutions. Any ideas?
    >>>>
    >>>>Many thanks,
    >>>>
    >>>>Phil
    >>>
    >>>I was thinking primarily in sed, because the rest of the shell script

    >>
    >>sub("the rest of the shell script","the author of the shell script") ;-)
    >>
    >>
    >>>finds sed more convenient than awk, and forgot that gawk extends
    >>>standard nawk with a toupper function. Dumb.

    >>
    >>nawk isn't standard but IIRC it does have toupper() as does any awk that
    >>conforms to the IEEE standard
    >>(http://www.opengroup.org/onlinepubs/...ities/awk.html).
    >>
    >> Ed.

    >


    > The rest of the shell script is mostly a long series of 's/.../.../'
    > commands,


    But maybe it doesn't have to be. If you go into a problem thinking of a
    sed solution, then a long series of 's/.../.../' is pretty much what
    you're guaranteed to end up with.

    > which are more convenient in sed than awk.


    That depends on what you're substituting. Look:

    $ x="b
    c"
    $ echo "aed" | sed 's/e/$x/'
    a$xd
    $ echo "aed" | sed "s/e/$x/"
    sed: -e expression #1, char 5: unterminated `s' command
    $ echo "aed" | awk -v x="$x" 'sub(/e/,x)'
    ab
    cd

    You'd have to modify your replacement string to get sed to behave in the
    natural way (i.e. the way awk behaves). This applies to various
    "special" characters, including the above newlines.

    > That has nothing
    > to do with the author of the script.


    We'll probably have to agree to disagree on that.

    > The awk book doesn't mention toupper, which is why I always forget
    > about it.
    >


    Yes it does, in section 8 on String Manipulation:
    http://www.gnu.org/software/gawk/man...ring-Functions

    Ed.

+ Reply to Thread

Similar Threads

  1. FAQ 4.30 How do I capitalize all the words on one line?
    By Application Development in forum Perl
    Replies: 0
    Last Post: 11-29-2007, 03:03 AM
  2. convert f77 code from mixed case to lower case
    By Application Development in forum Fortran
    Replies: 44
    Last Post: 07-27-2007, 01:09 AM
  3. FAQ 4.30 How do I capitalize all the words on one line?
    By Application Development in forum Perl
    Replies: 3
    Last Post: 07-16-2007, 02:12 PM
  4. FAQ 4.30 How do I capitalize all the words on one line?
    By Application Development in forum Perl
    Replies: 0
    Last Post: 04-20-2007, 08:03 AM
  5. To capitalize the first letter
    By Application Development in forum Graphics
    Replies: 1
    Last Post: 02-22-2006, 11:55 AM