10

我必须编写一个脚本,该脚本需要一个句子并打印字数、字符数(不包括空格)、每个单词的长度和长度。我知道单词中存在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
4

7 回答 7

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
于 2012-01-05T02:40:45.153 回答
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
于 2012-01-05T02:24:28.510 回答
4
#!/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;
于 2012-01-05T02:28:15.870 回答
4
string="i am a string"

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

echo $n

4

n 的值可以用作表达式中的整数

eg.

echo $((n+1))
5
于 2012-01-05T02:33:12.517 回答
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
于 2012-01-05T02:25:46.913 回答
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
于 2012-01-05T03:35:01.153 回答
0

代码

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

输出

3
于 2015-06-26T13:56:46.310 回答