Results 1 to 6 of 6

Thread: Bash uppercase after {. ! ?}

  1. #1
    Join Date
    Jan 2014
    Beans
    1

    Bash uppercase after {. ! ?}

    how can i convert the first letter after ".", "!" and "?" and new line to an uppercase letter?
    example: "hello. my name is newbe.can u help me?" to " Hello. My name is newbe.Can u help me?"

    i tried something with sed but it just convert just the first letter from new line
    sed 's/^\([A-Za-z]\)\([A-Z-a-z]\+\)/\U\1\L\2/'

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    perl?

    There's a way to do it in perl:

    Code:
    echo "hello. my name is newbe.can u help me?"  | perl -e 'while (<>) { s/^(\s*.)/\U$1/;s/([[:punct:]]\s*.)/\U$1/g;print}'
    There might be a more efficient way. Basically that is a short program jammed into a single line.

    Code:
    while (<>) {                            # read from stdin until no more input
      s/^(\s*.)/\U$1/;                   # substitute
      s/([[:punct:]]\s*.)/\U$1/g;  # global substitute, pattern starts with punctuation
      print
    }
    All that takes effect on the default variable of perl, $_ which is implied if it is not written.

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Bash uppercase

    Possible slightly more compact version using lookbehinds?

    Code:
    perl -pe 's/((?<!.)|(?<=[[:punct:]])\W*)\w/\U$&/g'
    (matches and uppercases any single word character that is preceded either by nothing or by punctuation and zero or more additional non-word characters)

  4. #4
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Bash uppercase after {. ! ?}

    Unnecessarily complex, I think. Perl's motto applies.

    Code:
    perl -pe 's/(^|[.!?])([a-z])/$1\U$2/g' # how I would write it using common idioms
    perl -pe 's/(?<![^.!?])\w/\U$&/g'      # code golf version

  5. #5
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash uppercase after {. ! ?}

    trent and steeldriver, thanks. I was looking for -p, it eliminates the while loop.
    Last edited by Lars Noodén; January 31st, 2014 at 03:29 PM. Reason: typo

  6. #6
    Join Date
    Sep 2010
    Beans
    62

    Re: Bash uppercase after {. ! ?}

    Code:
    # echo "hello. my name is newbe.can u help me?" | ruby -e 'puts gets.split(/\s*\.\s*/).map(&:capitalize).join(". ") '
    Hello. My name is newbe. Can u help me?

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
  •