此脚本检查不同文件中的不同单词。不早知道这个词,我该如何计算它的出现?
输出应为数值,如果它与给定文件中搜索到的单词匹配,否则应返回无效输出。
#!/bin/bash
if [ "Hello" -ne 1 ]
then
echo "Pass appropriate number of command line arguments"
else
if [ -e "$1" ]
then
if [ -f "$1" ]
then
if [ -r "$1" ]
then
grep -wc "Hello" $1
else
echo "Input file does not have read permission"
fi
else
echo "Input file is not an ordinary file"
fi
else
echo "Input file does not exist"
fi
fi
答案 0 :(得分:2)
要计算文件中的单词,您可以考虑创建此类文件
#!/bin/bash
#search_word.sh
cnt=$(grep "$1" "$2" -o | wc -l)
if [ "$cnt" -eq 0 ]
then
echo "This file has no word as $1"
else
echo "This file has $cnt times of the word $1"
fi
,然后从命令提示符处调用
$ . ./search_word.sh 'myword' myfile.txt
wc
代表word count
WORD
或WorD
或
word
被认为是相同的),然后在前面替换标记-o
| wc -l
与-io
WOR
时不计入Word
),请在前面替换标记-o
| wc -l
与-wio