Shell Script To Count English Language Articles Such As 'A', 'An' and 'The'

by on April 21, 2008 · 1 comment

This script also covers following techniques:
=> Reading input text file line by line
=> Reading and processing words in input
=> Convert word to lowercase

  1. #!/bin/bash
  2. # Write a shell script that counts English language articles (a, an, the)
  3. # in a given text file.
  4. #
  5. # --------------------------------------------------------------------
  6. # This is a free shell script under GNU GPL version 2.0 or above
  7. # Copyright (C) 2005 nixCraft project.
  8. # Feedback/comment/suggestions : http://cyberciti.biz/fb/
  9. # -------------------------------------------------------------------------
  10. # This script is part of nixCraft shell script collection (NSSC)
  11. # Visit http://bash.cyberciti.biz/ for more information.
  12. # -------------------------------------------------------------------------
  13. echo -n "Enter a file name : "
  14. read file
  15. a=0
  16. the=0
  17. an=0
  18.  
  19. # make sure file exist
  20. if [ ! -f $file ]
  21. then
  22. echo "$file not a file!"
  23. exit 1
  24. fi
  25.  
  26. # put while loop to read a $file
  27. while read line
  28. do
  29. #process each word
  30. for w in $line
  31. do
  32. # convert word to lowercase; so that we can count ThE, THE, the, THe etc all
  33. lword="$(echo $w | tr '[A-Z]' '[a-z]')"
  34.  
  35. # is it 'a' article?
  36. [ $lword = "a" ] && (( a++ )) || :
  37. [ $lword = "the" ] && (( the++ )) || :
  38. [ $lword = "an" ] && (( an++ )) || :
  39. done
  40. done < $file
  41.  
  42. # display stats
  43. echo "a article occured $a times"
  44. echo "the article occured $the times"
  45. echo "an article occured $an times"


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • sajan

    after runing the script in ubuntu it give a error that line io i.e the while command not found

Previous Script:

Next Script: