Uppercasing First Letter of Words Using SED
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"
Danf
Updated on July 09, 2022Comments
-
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 almost 12 yearsThis answer is most portable. Thank you.
-
Neil Townsend over 9 yearsThis seems to work on busybox, whereas the sed solutions don't.
-
Jonathan Komar over 8 yearsIt 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 over 8 yearsNote: only works with GNU sed. If you're in OSX, FreeBSD, etc. this does nothing.
-
Botond over 7 yearsthis 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 about 7 yearsCan you describe what the command is doing? The man page, for example, doesn't list this.
-
xizdaqrian about 7 yearsIt'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 over 5 years@JonathanKomar Edited as you requested :)
-
Gus about 5 yearsThe key here is the
\u
token, which will uppercase the next character. For a better reference see theEscape sequences
section of this link. -
Princekin about 4 yearscould you give some methods that work on macosx,thanks! @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 useawk
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 about 4 yearsI 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 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 about 3 yearsWorks on both OSX and GNU.
-
Chris almost 3 yearsFor those on macOS, you can use homebrew to easily install GNU sed which has this feature.
brew install gnu-sed
. Then just usegsed
instead ofsed
. -
gone almost 3 yearsNice 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 over 2 yearsN.B. This requires a Bash version 4 or later.
echo "$BASH_VERSION"
to check.