#!/bin/bash
if [ $# -lt 1]; then
echo "ERROR: you should input 2 words at least"
exit -1
fi
FILE=$1
WORDS_NUM=$#-1
for n in $(seq $WORD_NUM)
do
shift
sed -e "s/[^a-zA-Z]/\n/g" $FILE | grep -v ^$ | sort | uniq -c | grep "^$1$"
done
# 执行
> ./statistic_words.sh index.html tinylab linux python
175 tinylab
43 linux
3 python
说明:
- shift:把用户从命令行中传入的参数依次往后移动位置,并把当前参数作为第一个参数即$1
- grep -v “^$” : 过滤空行
- uniq -c : 去除重复行,并统计每个重复行的次数
- sed “s/a/b/g” test.txt : 在test.txt中查找a替换成b
本文介绍了如何使用bash脚本统计用户输入的两个及以上单词在指定文件中的出现频率,展示了shift、grep、uniq和sed等技术的应用。通过实例执行,得出tinylab、linux和python在'index.html'文件中的词频统计结果。
9299

被折叠的 条评论
为什么被折叠?



