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インタプリタを使用したい場合、最初の行は次のようになります-bashsh

#!/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 に答える