BASH Word Length Histogram

  • Following on the request for character counting in BASH, here is one that adds up the frequency of word lengths:

    for var in "[email protected]"; do
      #echo "${#var}"
      (( Letters[${#var}]++ ))
      #echo ${Letters[${#var}]}
    for i in ${!Letters[*]}; do
      echo $i " letters: " ${Letters[$i]}

  • And here is a sample output:

    ./ five six four three two one seven twenty onehundred eleven ten do a deer a female deer and that is how the cookie crumbles and i am pretty sure that you will find that this is a really useful bash script no matter how many things that you want to pass into its parameter fields
    1  letters:  4
    2  letters:  6
    3  letters:  12
    4  letters:  17
    5  letters:  2
    6  letters:  11
    8  letters:  1
    9  letters:  1
    10  letters:  1

Log in to reply