专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

shell脚本 实现计算英文文章里诸如'A/An/The'个数

百度收藏 QQ搜藏

原文: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"
上一篇:清除Lighttpd web服务器缓存的shell脚本 下一篇:Bash shell脚本:利用shell数组颠倒文本并输出

power by soyo123 2007-2008