2012-01-05 19 views
10

Devo scrivere uno script che accetta una frase e stampa il conteggio delle parole, il conteggio dei caratteri (esclusi gli spazi), la lunghezza di ogni parola e la lunghezza. So che esiste wc -m per contrastare il numero di caratteri nella parola, ma come utilizzarlo nella sceneggiatura?Couting di caratteri, parole, lunghezza delle parole e lunghezza totale in una frase

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

Suona come [tag: compiti a casa]. Se lo è, per favore taggalo come tale.Per molti di noi, se vediamo quel tag, faremo uno sforzo maggiore per aiutarti a capire perché la risposta è quella che è, piuttosto che fornire una soluzione. – ghoti

+0

grazie, ghoti, parlerò ulteriormente. Ma mi sto solo esercitando con script e comandi shell – mydreamadsl

+0

E qual è la tua domanda? – Raedwald

risposta

12

riffing sulla risposta di Jaypal Singh:

[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

grazie per la tua risposta, come posso produrre solo la lunghezza massima? – mydreamadsl

+0

cosa intendi per "lunghezza massima"? –

+0

come token "tre" è la parola più grande nella stringa e la sua lunghezza è 5 – mydreamadsl

10
echo $mystring | wc -w 

o

echo $mystring | wc --words 

farà un conteggio di parola per voi.

È possibile reindirizzare ogni parola di wc:

echo $token | wc -m 

per memorizzare il risultato in una variabile:

mycount=`echo $token | wc -m` 
echo $mycount 

per aggiungere al totale, come si va parola per parola, fare matematica con questo sintassi:

total=0 
#start of your loop 
total=$((total+mycount)) 
#end of your loop 
echo $total 
2

Sei molto vicino. In bash puoi usare # per ottenere la lunghezza della tua variabile.

Inoltre, se si desidera utilizzare utilizzare bash interprete bash invece di sh e la prima linea va in questo modo -

#!/bin/bash

Usa questo script -

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

Ci sono un paio di modi per fare aritmetica nella shell. Mi piace '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

Wow, cool, grazie!) Come posso controllare la lunghezza massima della parola in una stringa intera? – mydreamadsl

3
string="i am a string" 

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

echo $n 

4 

Il valore di n può essere usato come un intero nelle espressioni

eg. 

echo $((n+1)) 
5 
0

Il comando wc è una buona scommessa.

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

dove il risultato è il numero di linee, parole e caratteri. In uno script:

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

Il declare incorporato getta una variabile come un numero intero qui, in modo che += aggiungerà piuttosto che aggiungere.

$ ./doit 
lines: 1 
words: 5 
chars: 24 
word lengths: 3 3 5 4 4 
nonspace chars: 19 
0

codice

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

uscita

3 
+2

Una breve spiegazione potrebbe migliorare questa risposta. –

+2

Lo spiegherò presto. Per prima cosa ho bisogno di rimuovere la mia appendice (seriamente). – Alan

Problemi correlati