一。查一个文件
命令:
grep -w '所查词' 文本文件 | tr -c [:alpha:] "/n" | grep '所查词' -c
例子:
查test.txt中的hello出现的次数
grep -w 'hello' test.txt | tr -c [:alpha:] "/n" | grep 'hello' -c
二。查一批类似文件
windows下(安装了msys等)
for %f in (*.txt) do grep -w '所查词' %f | tr -c [:alpha:] "/n" | grep '所查词' -c
类unix下
可以用如下脚本:
#!/bin/sh
for i in *.txt ; do
echo $i;grep 'hello' $i| tr -c [:alpha:] "/n"| grep 'hello' -c
done
本文介绍了一种在特定文本文件中搜索指定词汇的方法,并提供了在单个文件及多个文件中查找单词出现次数的具体步骤。通过使用grep结合tr和管道命令,可以有效地统计目标词汇的频率。
333

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



