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

后端 未结 7 1794
别跟我提以往
别跟我提以往 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:52

    #!/bin/bash
    
    mystring="one two three test five"
    for token in $mystring; do
      echo -n "$token: ";
      echo -n $token | wc -m;
    done
    echo "--------------------------";
    echo -n "Total words: ";
    echo "$mystring" | wc -w;
    echo -n "Total chars: ";
    echo "$mystring" | wc -m;
    

提交回复
热议问题