计算句子中的字符,单词,单词长度和总长度

时间:2012-01-05 02:21:09

标签: shell wc

我必须编写一个脚本,它接受一个句子并打印字数,字符数(不包括空格),每个单词的长度和长度。我知道有wc -m来对抗单词中的字符数,但是如何在脚本中使用它?

#!/bin/bash

mystring="one two three test five"
maxlen=0;
for token in $mystring; do
  echo -n "$token: ";
  echo -n $token | wc -m;
    if [ ${#token} -gt $maxlen ]; then 
      maxlen=${#token}; fi;
done

echo "--------------------------";
echo -n "Total words: ";
echo "$mystring" | wc -w;
echo -n "Total chars: ";
echo "$mystring" | wc -m;
echo -n "Max length: "; 
echo $maxlen

7 个答案:

答案 0 :(得分:12)

对Jaypal Singh的回答嗤之以鼻:

jcomeau@intrepid:~$ mystring="one two three four five"
jcomeau@intrepid:~$ echo "string length: ${#mystring}"
string length: 23
jcomeau@intrepid:~$ echo -n "lengths of words: "; i=0; for token in $mystring; do echo -n "${#token} "; i=$((i+1)); done; echo; echo "word count: $i"
lengths of words: 3 3 5 4 4 
word count: 5
jcomeau@intrepid:~$ echo -n "maximum string length: "; maxlen=0; for token in $mystring; do if [ ${#token} -gt $maxlen ]; then maxlen=${#token}; fi; done; echo $maxlen
maximum string length: 5

答案 1 :(得分:10)

echo $mystring | wc -w

echo $mystring | wc --words

会为你做一个字数。

您可以将每个单词传递给wc:

echo $token | wc -m

将结果存储在变量中:

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

要逐字逐句添加到总数中,请使用以下语法进行数学运算:

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

答案 2 :(得分:4)

string="i am a string"

n=$(echo $string | wc -w )

echo $n

4

n的值可以用作表达式

中的整数
eg.

echo $((n+1))
5

答案 3 :(得分:3)

#!/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;

答案 4 :(得分:2)

你非常接近。在bash中,您可以使用#来获取变量的长度。

另外,如果您想使用bash口译员,请使用bash代替sh,第一行就是这样 -

#!/bin/bash

使用此脚本 -

#!/bin/bash

mystring="one two three test five"
for token in $mystring
do
    if [ $token = "one" ]
    then
        echo ${#token}
    elif [ $token = "two" ]
    then
        echo ${#token}
    elif [ $token = "three" ]
    then
        echo ${#token}
    elif [ $token = "test" ]
    then
        echo ${#token}
    elif [ $token = "five" ]
    then
        echo ${#token}
    fi
done

答案 5 :(得分:0)

wc命令是一个不错的选择。

$ echo "one two three four five" | wc
       1       5      24

其中结果是行数,单词和字符。在脚本中:

#!/bin/sh

mystring="one two three four five"

read lines words chars <<< `wc <<< $mystring`

echo "lines: $lines"
echo "words: $words"
echo "chars: $chars"

echo -n "word lengths:"
declare -i nonspace=0
declare -i longest=0
for word in $mystring; do
  echo -n " ${#word}"
  nonspace+=${#word}"
  if [[ ${#word} -gt $longest ]]; then
    longest=${#word}
  fi
done
echo ""
echo "nonspace chars: $nonspace"
echo "longest word: $longest chars"

declare内置在此处将变量转换为整数,因此+=将添加而不是追加。

$ ./doit
lines: 1
words: 5
chars: 24
word lengths: 3 3 5 4 4
nonspace chars: 19

答案 6 :(得分:0)

<强>码

var=(one two three)
length=${#var[@]}
echo $length

<强>输出

3