在 unix 中查找作为输入从命令行输入的单词的出现次数 [英] To find number of occurrences of a word taken as input from command line in unix
本文介绍了在 unix 中查找作为输入从命令行输入的单词的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于包含
Apple fruit Apple tree
Tree AApple AApklle Apple apple
TREE
Apple
我想找出单词 Apple
出现的次数.输出应该是 4
.
我的 script.sh 文件包含
I want to find number of occurrences of the word Apple
. Output should be 4
.
My script.sh file contains
#!/bin/bash
FILE="$1"
TOFIND="$2"
if [ -f "$FILE" ];
then
grep -o '<"$TOFIND">' "$FILE" | wc -l
fi
当我尝试使用
bash script.sh file1.txt Apple
输出显示 0
.请帮忙解决这个问题.
The output shows 0
. Please help solve this problem.
推荐答案
您可以将 grep 行更改为:
You can change the grep line to:
grep -o '<'"$TOFIND"'>' "$FILE" | wc -l
或者只是:
grep -o "<$TOFIND>" "$FILE" | wc -l
然后就可以了.这是因为引号,你的双引号是在单引号里面引用的,所以它们没有展开.
Then it will work. It's because the quotes, your double quotes was quoted inside single quotes, so they are not expanded.
这篇关于在 unix 中查找作为输入从命令行输入的单词的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文