Counting characters, words, length of the words and total length in a sentence

后端 未结 7 1895
别跟我提以往
别跟我提以往 2021-02-20 15:08

I have to write a script that takes a sentence and prints the word count, character count (excluding the spaces), length of each word and the length. I know that there exist

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-20 15:45

    echo $mystring | wc -w
    

    or

    echo $mystring | wc --words
    

    will do a word count for you.

    You can pipe each word to wc:

    echo $token | wc -m
    

    to store the result in a variable:

    mycount=`echo $token | wc -m`
    echo $mycount
    

    to add to the total as you go word by word, do math with this syntax:

    total=0
    #start of your loop
    total=$((total+mycount))
    #end of your loop
    echo $total
    

提交回复
热议问题