1. 统计文件每个单词数量
#!/bin/bash
function count()
{
if [ $# != 1 ]
then
echo "Need one file parameter to work!"
exit 1;
fi
# 删除标点符号和特殊字符
tr -d '[:punct:]' < $1 | tr -d '[:cntrl:]' | \
# 删除所有数字
tr -d '[:digit:]' | \
# 所有大写转换为小写
tr '[:upper:]' '[:lower:]' | \
# 把重复空格替换为一个空格
tr -s ' ' | \
# 把空格替换为换行
tr ' ' '\n' | \
# 把相同单词放到一起
sort | \
# 删除相同单词, 并进行统计
uniq -c | \
# 根据重复次数进行排序
sort -rn
}
while true
do
read -p "Enter the file path(or quit)"
case "$REPLY" in
[Qq]|[Qq][Uu][Ii][Tt])
echo "Bye"
exit 0
;;
*)
# 判断是否为普通文件,可读,内容不为空
if [ -f "$REPLY" ] && [ -r "$REPLY" ] && [ -s "$REPLY" ]
then
count "$REPLY"
else
echo "$REPLY can not be dealed with"
fi
;;
esac
done