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

7

10

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
Ellsworthellwood answered 5/1, 2012 at 2:21 Comment(4)
Sounds like homework. If it is, please tag it as such. For many of us, if we see that tag, we'll make a greater effort to help you learn why the answer is what it is, rather than just providing a solution.Leo
thank you, ghoti, will mention further. But i am just practising with scripts and shell commandsEllsworthellwood
And what is your question?Playlet
The one liner for the CLI: echo 'Here is my test String' | wc -mAmplexicaul
R
12

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
Rectilinear answered 5/1, 2012 at 2:40 Comment(4)
thanks for your answer, how can i output only maximum length?Ellsworthellwood
what do you mean by "maximum length"?Rectilinear
like token "three" is the largest word in the string, and it's length is 5Ellsworthellwood
Sorry buddy out for dinner but looks like you are all set! ;) Good luck!!Discommend
A
10
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
Arguable answered 5/1, 2012 at 2:24 Comment(0)
C
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;
Canoe answered 5/1, 2012 at 2:28 Comment(1)
Wow, cool, thanks!) how can i check maximum length of the word in whole string?Ellsworthellwood
H
4
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
Hearne answered 5/1, 2012 at 2:33 Comment(1)
would you please make this as one-liner tooSpearwort
D
2

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
Discommend answered 5/1, 2012 at 2:25 Comment(1)
There are a couple ways to do arithmetic in the shell. I like foo=$((bar + 1))Ladino
L
0

The wc command is a good bet.

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

where the result is number of lines, words and characters. In a 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"

The declare built-in casts a variable as an integer here, so that += will add rather than append.

$ ./doit
lines: 1
words: 5
chars: 24
word lengths: 3 3 5 4 4
nonspace chars: 19
Leo answered 5/1, 2012 at 3:35 Comment(0)
P
0

code

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

output

3
Phelips answered 26/6, 2015 at 13:56 Comment(2)
A brief explanation would improve this answer.Tequila
I'll soon explain it. First I need to remove my appendix (seriously).Phelips

© 2022 - 2024 — McMap. All rights reserved.