原文:http://bash.cyberciti.biz/file-management/counts-english-language-articles/
这个shell脚本还有下面功能:
=>逐行读取输入文件 =>读取并处理输入词句 => 转换所有词句为小写字母
以下是该shell脚本的源代码:
#!/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" |