Shell Script To Count English Language Articles Such As 'A', 'An' and 'The'
Posted in Decision Making » File-management
This script also covers following techniques:
=> Reading input text file line by line
=> Reading and processing words in input
=> Convert word to lowercase
#!/bin/bash # Write a shell script that counts English language articles (a, an, the) # in a given text file. # # -------------------------------------------------------------------- # This is a free shell script under GNU GPL version 2.0 or above # Copyright (C) 2005 nixCraft project. # Feedback/comment/suggestions : http://cyberciti.biz/fb/ # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- echo -n "Enter a file name : " read file a=0 the=0 an=0 # make sure file exist if [ ! -f $file ] then echo "$file not a file!" exit 1 fi # put while loop to read a $file while read line do #process each word for w in $line do # convert word to lowercase; so that we can count ThE, THE, the, THe etc all lword="$(echo $w | tr '[A-Z]' '[a-z]')" # is it 'a' article? [ $lword = "a" ] && (( a++ )) || : [ $lword = "the" ] && (( the++ )) || : [ $lword = "an" ] && (( an++ )) || : done done < $file # display stats echo "a article occured $a times" echo "the article occured $the times" echo "an article occured $an times"
Download - Email this to a friend - Printable version
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: english language articles, exit 1, fi, for loop, if command, loop method, shell loops, shell script, while loop ~ Last updated on: April 21, 2008

