How can I capitalize the first letter of each word?

前端 未结 20 2325
暖寄归人
暖寄归人 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
    
    0 讨论(0)
  • 2021-01-14 12:44

    You can do it with Ruby too, using the command line format in a terminal:

    cat FILENAME | ruby -n -e 'puts gsub(/\b\w/, &:upcase)'
    

    Or:

    ruby -e 'puts File.read("FILENAME").gsub(/\b\w/, &:upcase)'
    
    0 讨论(0)
  • 2021-01-14 12:46

    Here's another Ruby solution, using Ruby's nice little one-line scripting helpers (automatic reading of input files etc.)

    ruby -ni~ -e "puts $_.gsub(/\b\w+\b/) { |word| word.capitalize }" foo.txt
    

    (Assuming your text is stored in a file named foo.txt.)

    Best used with Ruby 1.9 and its awesome multi-language support if your text contains non-ASCII characters.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-14 12:48

    Although it was mentioned in the comments, nobody ever posted the awk approach to this problem:

    $ cat output.txt
    this is my first sentence. and this is the second sentence. that one is the third.
    $
    $ awk '{for (i=0;i<=NF;i++) {sub(".", substr(toupper($i), 1,1) , $i)}} {print}' output.txt 
    This Is My First Sentence. And This Is The Second Sentence. That One Is The Third.
    

    Explanation

    We loop through fields and capitalize the first letter of each word. If field separator was not a space, we could define it with -F "\t", -F "_" or whatever.

    0 讨论(0)
  • 2021-01-14 12:50
    VB.Net:
    
    Dim sr As System.IO.StreamReader = New System.IO.StreamReader("c:\lowercase.txt")
    Dim str As String = sr.ReadToEnd()
    sr.Close()
    str = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str)
    Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("c:\TitleCase.txt")
    sw.Write(str)
    sw.Close()
    
    0 讨论(0)
提交回复
热议问题