Unix shell脚本,用于从文本文件中查找特定单词的出现次数

Unix shell脚本,用于从文本文件中查找特定单词的出现次数,unix,Unix,我的文件是ss.txt Another instance started Another instance started Another instance started Another instance started No instance started No instance started No instance started No instance started 如果我像这样使用shell脚本程序 #!/bin/sh t=0 #temp=0 echo "Enter file

我的文件是ss.txt

Another instance started
Another instance started 
Another instance started
Another instance started
No instance started
No instance started
No instance started
No instance started
如果我像这样使用shell脚本程序

#!/bin/sh
t=0
#temp=0
echo "Enter filename"
read f1
if [ -f $f1 ]
then
echo "1.count char,words,lines"
echo "2.particular word to find in file"
echo "3.exit"
echo "Enter ur choice?"
read ch
case $ch in
1) wc -c $f1
   echo "characters"
   wc -w $f1
   echo "words"
   wc -l $f1
   echo "lines" ;;
2) echo "Enter the word whose occurence has to be found"
   read c
   t='echo $f1 | wc -c'
   echo $t ;;
3) exit ;;
esac
else
echo "File does not exist"
fi
如果我运行这段代码,我会得到以下输出 我可以得到正确的选项2,即单词出现不正确

我觉得

Enter filename
ss.txt
1.count char,words,lines
2.particular word in file
3.exit
Enter ur choice?
1
180 ss.txt
characters
24 ss.txt
words
8 ss.txt
lines
Enter filename
ss.txt
1.count char,words,lines
2.particular word in file
3.exit
Enter ur choice?
2
Enter the word whose occurence has to be found
Another
0
这个我得到的是正确的,但是对于选择2,我得到的是

Enter filename
ss.txt
1.count char,words,lines
2.particular word in file
3.exit
Enter ur choice?
1
180 ss.txt
characters
24 ss.txt
words
8 ss.txt
lines
Enter filename
ss.txt
1.count char,words,lines
2.particular word in file
3.exit
Enter ur choice?
2
Enter the word whose occurence has to be found
Another
0
请参见此处的“我得到零”,但输出应为4,更改为:

t='echo $f1 | wc -c'


请根据您的要求尝试以下命令

t=
grep-c“text”“${f1}”
(-c,--每个输入文件的匹配行计数)

t=
grep-o“text”“${f1}”
(-o,仅显示匹配行中与模式匹配的部分。)

bcsmc2rtese001[~/Shell\p]$grep-c“另一个”ss.txt
4
bcsmc2rtese001[~/Shell\u p]$grep-o“另一个”ss.txt
另一个
另一个
另一个

另一个

我试过了我得到了像bb.sh:28:4:没有找到,我也得到了0只作为回答你确定吗,你只更改了那一行,没有其他更改吗?上面的代码可以运行17000行吗?请回答实际上是我要在其中尝试此代码的日志文件,并且日志文件一直在运行。。代码是否适用于正在运行的线路?您是否面临任何问题?