Uppercasing First Letter of Words Using SED

53,075

Solution 1

This line should do it:

sed -e "s/\b\(.\)/\u\1/g"

Solution 2

Using awk:

awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1' file

The output would be:

Trouble Me
Gold Rush Brides

Solution 3

Use the following sed command for capitalizing the first letter of the each word.

echo -e "Trouble me \nGold rush brides" | sed -r 's/\<./\U&/g'

output

Trouble Me
Gold Rush Brides

The -r switch tells sed to use extended regular expressions. The instructions to sed then tell it to "search and replace" (the s at the beginning) the pattern \<. with the pattern \U& globally, i.e. all instances in every line (that's the g modifier at the end). The pattern we're searching for is \<. which is looking for a word boundary (\<) followed by any character (.). The replacement pattern is \U&, where \U instructs sed to make the following text uppercase and & is a synonym for \0, which refers to "everything that was matched". In this case, "everything that was matched" is just what the . matched, as word boundaries are not included in the matches (instead, they are anchors). What . matched is just one character, so this is what is upper cased.

Solution 4

I had apostrophes so, working off the first solution...

mike@mike-laptop3:~$ echo "BEST WESTERN PLUS BOB's INN" | tr "[A-Z]" "[a-z]" | sed -e "s/\b\(.\)/\u\1/g"

Best Western Plus Bob'S Inn

mike@mike-laptop3:~$ echo "BEST WESTERN PLUS BOB's INN" | tr "[A-Z]" "[a-z]" | sed "s/\( \|^\)\(.\)/\1\u\2/g"

Best Western Plus Bob's Inn

Solution 5

Another shorter version with sed:

sed -e "s/\b./\u\0/g"
Share:
53,075
Danf
Author by

Danf

Updated on July 09, 2022

Comments

  • Danf
    Danf almost 2 years

    How do you replace the first letter of a word into Capital letter, e.g.

    Trouble me
    Gold rush brides
    

    into

    Trouble Me
    Gold Rush Brides
    
  • yrk
    yrk almost 12 years
    This answer is most portable. Thank you.
  • Neil Townsend
    Neil Townsend over 9 years
    This seems to work on busybox, whereas the sed solutions don't.
  • Jonathan Komar
    Jonathan Komar over 8 years
    It is so much nicer when I don't have to do man sed to understand answers, because the answer contains an explanation for its contents. Maybe you would consider providing an explanation for your answer?
  • ghoti
    ghoti over 8 years
    Note: only works with GNU sed. If you're in OSX, FreeBSD, etc. this does nothing.
  • Botond
    Botond over 7 years
    this helps as well if you have capital letters all over, but you only want the FIRST letter of each word to be capital
  • Ken Sharp
    Ken Sharp about 7 years
    Can you describe what the command is doing? The man page, for example, doesn't list this.
  • xizdaqrian
    xizdaqrian about 7 years
    It's telling sed to convert the character after each word boundry to uppercase. \b is word boundry. Check out www.rexegg.com for some great tables of useful regular expression symbols.
  • dionyziz
    dionyziz over 5 years
    @JonathanKomar Edited as you requested :)
  • Gus
    Gus about 5 years
    The key here is the \u token, which will uppercase the next character. For a better reference see the Escape sequences section of this link.
  • Princekin
    Princekin about 4 years
    could you give some methods that work on macosx,thanks! @ghoti
  • ghoti
    ghoti about 4 years
    @lslboy, not an easy task in sed without GNU sed's \u to force case on the match on-the-fly. One could conceivably do this in a longer sed script, but a different language would let you do it more easily. For shell access and portability, I'd probably use awk instead. awk '{for(i=1;i<=NF;i++){sub(".",toupper(substr($i,1,1)),$i)}}1' might be sufficient. If you need to maintain word boundaries, then more code is required, but it's still easier in awk than in sed.
  • Princekin
    Princekin about 4 years
    I find a easy way in macosx,you can try it, echo NaMe is | perl -pe 's/([a-z])/\u\1/g' it work for me in macosx!
  • ghoti
    ghoti about 4 years
    @lslboy .. not quite. The task was to capitalize just the first letter of each word, and your solution capitalizes everything. the tr(1) command could likely do that more easily. With perl, consider instead, perl -pe 's/\b(.)/\u\1/g'. The \b identifies a word boundary. But note that this still uses \u, which is a Perl RE extension, not supported universally by non-perl tools like sed.
  • Ike Chang
    Ike Chang about 3 years
    Works on both OSX and GNU.
  • Chris
    Chris almost 3 years
    For those on macOS, you can use homebrew to easily install GNU sed which has this feature. brew install gnu-sed. Then just use gsed instead of sed.
  • gone
    gone almost 3 years
    Nice solution! (Use -E instead of -r on MacOS.) I'm using this from a Makefile (bmake) and the command I used is slightly different: CPP_STYLE_NS!=bash -c 'export CLASS_NAME="${CLASS_NAME}"; echo $${CLASS_NAME} | sed -E "s/^./$$(tr [:lower:] [:upper:] <<< $${CLASS_NAME:0:1})/g"' (Massage a given name string.)
  • bleater
    bleater over 2 years
    N.B. This requires a Bash version 4 or later. echo "$BASH_VERSION" to check.