How can I capitalize the first letter of each word?

前端 未结 20 2361
暖寄归人
暖寄归人 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:47

    Another solution with awk, pretending to be simpler instead of being shorter ;)

    $ cat > file
      thanks for all the fish
      ^D
    $ awk 'function tocapital(str) {
               if(length(str) > 1)
                   return toupper(substr(str, 1, 1)) substr(str,2)
               else
                   return toupper(str)
           }
    
           {
               for (i=1;i<=NF;i++) 
                   printf("%s%s", tocapital($i), OFS);
               printf ORS
           }
          ' < file
      Thanks For All The Fish
    

提交回复
热议问题