How do you replace the first letter of a word into Capital letter, e.g.
Trouble me
Gold rush brides
into
Trouble Me
Gold R
Using sed with tr:
name="athens"
echo $name | sed -r "s/^[[:alpha:]]/$(echo ${name:0:1} | tr [[:lower:]] [[:upper:]])/"
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
I know you said sed, but for shell scripting one of the easiest and more flexible for me was using Python which is available in most systems:
$ echo "HELLO WORLD" | python3 -c "import sys; print(sys.stdin.read().title())"
Hello World
For example:
$ lorem | python3 -c "import sys; print(sys.stdin.read().title())"
Officia Est Omnis Quia. Nihil Et Voluptatem Dolor Blanditiis Sit Harum. Dolore Minima Suscipit Quaerat. Soluta Autem Explicabo Saepe. Recusandae Molestias Et Et Est Impedit Consequuntur. Voluptatum Architecto Enim Nostrum Ut Corrupti Nobis.
You can also use things like strip()
to remove spaces, or capitalize()
:
$ echo " This iS mY USER ${USER} " | python3 -c "import sys; print(sys.stdin.read().strip().lower().capitalize())"
This is my user jenkins