2012-01-05 8 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

토큰 "3"은 문자열에서 가장 큰 단어이고 길이는 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에서는 #을 사용하여 변수 길이를 얻을 수 있습니다. 또한

, 당신은 bash 인터프리터 대신 shbash을 사용하여 사용하려는 첫 번째 줄은 다음과 같이가는 경우 -이 스크립트

#!/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 = $ ((bar + 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 싱의 대답에 riffing 표현

eg. 

echo $((n+1)) 
5 
0

wc 명령은 좋은 내기이다.

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

여기서 결과는 줄 수, 단어 및 문자 수입니다. += 추가보다는 추가합니다 있도록 declare 내장이 여기에 정수로 변수를 캐스트

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

: 스크립트에서. [: 숙제 태그]

$ ./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