Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting characters, words, length of the words and total length in a sentence

Tags:

shell

wc

I have to write a script that takes a sentence and prints the word count, character count (excluding the spaces), length of each word and the length. I know that there exist wc -m to counter number of characters in the word, but how to make use of it in script?

#!/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
like image 611
mydreamadsl Avatar asked Jan 05 '12 02:01

mydreamadsl


People also ask

How do you calculate the length of a word?

To check word count, simply place your cursor into the text box above and start typing. You'll see the number of characters and words increase or decrease as you type, delete, and edit them. You can also copy and paste text from another program over into the online editor above.


5 Answers

#!/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;
like image 71
Eugen Rieck Avatar answered Oct 23 '22 10:10

Eugen Rieck


string="i am a string"

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

echo $n

4

The value of n can be used as an integer in expressions

eg.

echo $((n+1))
5
like image 37
daya Avatar answered Oct 23 '22 10:10

daya


riffing on Jaypal Singh's answer:

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
like image 21
jcomeau_ictx Avatar answered Oct 23 '22 10:10

jcomeau_ictx


echo $mystring | wc -w

or

echo $mystring | wc --words

will do a word count for you.

You can pipe each word to wc:

echo $token | wc -m

to store the result in a variable:

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

to add to the total as you go word by word, do math with this syntax:

total=0
#start of your loop
total=$((total+mycount))
#end of your loop
echo $total
like image 43
Adam Dymitruk Avatar answered Oct 23 '22 12:10

Adam Dymitruk


You are very close. In bash you can use # to get the length of your variable.

Also, if you want to use bash interpreter use bash instead of sh and the first line goes like this -

#!/bin/bash

Use this 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
like image 25
jaypal singh Avatar answered Oct 23 '22 12:10

jaypal singh