在UNIX中查找作为命令行输入的单词出现的次数 [英] To find number of occurrences of a word taken as input from command line in unix
本文介绍了在UNIX中查找作为命令行输入的单词出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于文件 file1.txt
,其中包含
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屋!
查看全文