2012-01-05 76 views
10

我必须编写一个脚本,它需要一个句子并输出字数,字符数(不包括空格),每个单词的长度和长度。我知道存在wc -m来计数单词中的字符数,但是如何在脚本中使用它?Couting characters,words,lenght of the words and total lenght in a sentence

#!/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 
+0

听起来[标签:家庭作业。如果是这样,请标记为。对于我们中的很多人来说,如果我们看到这个标签,我们会做出更大的努力来帮助您了解答案是什么,而不仅仅是提供解决方案。 – ghoti 2012-01-05 02:56:22

+0

谢谢,ghoti,会进一步提及。但我只是练习脚本和shell命令 – mydreamadsl 2012-01-05 02:59:26

+0

你的问题是什么? – Raedwald 2013-07-09 19:55:51

回答

12

riffing上Jaypal辛格的回答是:

[email protected]:~$ mystring="one two three four five" 
[email protected]:~$ echo "string length: ${#mystring}" 
string length: 23 
[email protected]:~$ 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 
[email protected]:~$ 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 
+0

感谢您的回答,我如何输出最大长度? – mydreamadsl 2012-01-05 02:45:09

+0

“最大长度”是什么意思? – 2012-01-05 02:46:31

+0

喜欢标记“三”是字符串中最大的单词,它的长度是5 – mydreamadsl 2012-01-05 02:47:15

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

您已经非常接近。在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 
+0

有几种方法可以在shell中进行算术运算。我喜欢'foo = $((bar + 1))' – mkb 2012-01-05 02:29:09

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; 
+0

哇,很酷,谢谢!)如何检查整个字符串中单词的最大长度? – mydreamadsl 2012-01-05 02:38:16

3
string="i am a string" 

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

echo $n 

4 

n的值可以是作为一个整数表达式

eg. 

echo $((n+1)) 
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 
0

代码

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

输出

3 
+2

简短的解释会改善这个答案。 – 2015-06-27 02:12:27

+2

我会尽快解释它。首先,我需要删除我的附录(严重)。 – Alan 2015-06-28 17:26:42