2012-01-05 17 views
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 
+0

は、[:宿題タグ]のように聞こえます。そうであれば、そのようにタグ付けしてください。私たちの多くは、そのタグを見れば、ソリューションを提供するのではなく、なぜその答えが何であるかを知るための努力をします。 – ghoti

+0

ありがとう、ghoti、さらに言及します。しかし、私はちょうどスクリプトとシェルコマンドで練習しています – mydreamadsl

+0

あなたの質問は何ですか? – Raedwald

答えて

12

に整数として使用:

[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

+0

「最大長」はどういう意味ですか? –

+0

のようなトークン "three"は文字列中の最大の単語であり、長さは5 – mydreamadsl

10
echo $mystring | wc -w 

または

echo $mystring | wc --words 

あなたのためのワードカウントを行います。

あなたはトイレにパイプ各単語を行うことができます

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では、#を使用して変数の長さを取得できます。また

、あなたの代わりにshbashインタプリタの使用bashを使用すると、最初の行はこのようになっている場合 -

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

シェルで算術演算を行うには、いくつかの方法があります。私は 'foo = $((バー+1))'のように – mkb

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

3
string="i am a string" 

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

echo $n 

4 

nの値は、 Jaypalシンの答えにリフ表現

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

簡単な説明はこの回答を改善するでしょう。 –

+2

私はすぐにそれを説明します。まず、私の付録を削除する必要があります(真剣に)。 – Alan

関連する問題