How can I capitalize the first letter of each word?

前端 未结 20 2564
暖寄归人
暖寄归人 2021-01-14 12:31

I need a script in any language to capitalize the first letter of every word in a file.

Thanks for all the answers.

Stackoverflow rocks!

20条回答
  •  天命终不由人
    2021-01-14 12:43

    If using pipes and Python:

    $ 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
    

提交回复
热议问题