如何计算文件中给定单词的出现次数?

时间:2019-03-03 10:53:54

标签: bash shell unix

问题

此脚本检查不同文件中的不同单词。不早知道这个词,我该如何计算它的出现?

输出

输出应为数值,如果它与给定文件中搜索到的单词匹配,否则应返回无效输出。

#!/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

1 个答案:

答案 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
  • 如果您要不区分大小写地进行计数(例如WORDWorDword被认为是相同的),然后在前面替换标记-o | wc -l-io
  • 如果您寻找任何单词的完全匹配项(例如,在搜索WOR时不计入Word),请在前面替换标记-o | wc -l-wio