Results 1 to 9 of 9

Thread: sed: capitalizing the first letter of a line

  1. #1
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    sed: capitalizing the first letter of a line

    Hello guys,

    I'd like to instruct sed to capitalize the first letter of every line. I've gotten this far:

    Code:
    $ echo "the first word is not capitalized." | sed 's/^t/T/'
    The first word is not capitalized.
    That seems to work (yay!). Say I'd like to do it for multiple different lines of text, then of course I could do something like this:
    Code:
    $ cat bigstory.txt | sed 's/^a/A/; s/^b/B/; s/^c/C/; s/^d/D/; s/^e/E/'
    etcetera, and make a huge command. But that is not very elegant. There has to be a nice way to do it. Do you know one?
    Nunca te acostarás, sin saber una cosa más.

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed: capitalizing the first letter of a line

    I know I'm not supposed to say this but... Google is your friend, really.

    Code:
    sed -e 's/^\(.\)/\U\1/g' foo.txt
    This seems to be non-standard, I do not know the standard way.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Apr 2012
    Beans
    5,339

    Re: sed: capitalizing the first letter of a line

    There's a special instruction \u for that - combined with the & which substitutes the whole pattern found

    Code:
    $ echo "the first word is not capitalized." | sed 's/.*/\u&/'
    The first word is not capitalized.
    FYI, to capitalize the whole thing it's \U

    Code:
    $ echo "the first word is not capitalized." | sed 's/.*/\U&/'
    THE FIRST WORD IS NOT CAPITALIZED.

  4. #4
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed: capitalizing the first letter of a line

    Thanks guys. I'm learning how to use the command line, this helps. sed seems to be a difficult command to work with, the way it takes arguments appears so random.

    Quote Originally Posted by Bachstelze View Post
    I know I'm not supposed to say this but... Google is your friend, really.
    I'll try to do that more in the future.
    Nunca te acostarás, sin saber una cosa más.

  5. #5
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed: capitalizing the first letter of a line

    sed is not just a "command", for most intents and purposes you can treat it as a programming language in its own right.
    「明後日の夕方には帰ってるからね。」


  6. #6
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,721
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: sed: capitalizing the first letter of a line

    Quote Originally Posted by Bachstelze View Post
    I know I'm not supposed to say this but... Google is your friend, really.

    Code:
    sed -e 's/^\(.\)/\U\1/g' foo.txt
    This seems to be non-standard, I do not know the standard way.
    Why do you say it's non-standard? And why have g when it's just the first occurrence in a line that needs to be changed which seems to be taken care of by ^?
    de gustibus et coloribus non est disputandum -- Wiktionary

  7. #7
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed: capitalizing the first letter of a line

    Quote Originally Posted by vasa1 View Post
    Why do you say it's non-standard? And why have g when it's just the first occurrence in a line that needs to be changed which seems to be taken care of by ^?
    I say it is non-standard because it does not work on non-GNU versions of sed. I use /g because I am not very good at sed.
    Last edited by Bachstelze; October 27th, 2013 at 09:27 PM.
    「明後日の夕方には帰ってるからね。」


  8. #8
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,721
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: sed: capitalizing the first letter of a line

    Quote Originally Posted by Bachstelze View Post
    I say it is non-standard because it does not work on non-GNU versions of sed. ...
    Thanks, I didn't know that
    de gustibus et coloribus non est disputandum -- Wiktionary

  9. #9
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: sed: capitalizing the first letter of a line

    Well look at that. I ask a dumb question an now we've all learned something new. You're welcome. Hehe.

    In the main time, I found this website that is very useful for sed: http://www.grymoire.com/Unix/Sed.html
    Nunca te acostarás, sin saber una cosa más.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •