在不使用命令的情况下统计shell中文件中单词的出现次数
下面是我的代码,但它没有给出我想要的输出。 我想打印word及其出现编号,如: 安尼什3 曼尼什4在不使用命令的情况下统计shell中文件中单词的出现次数,shell,Shell,下面是我的代码,但它没有给出我想要的输出。 我想打印word及其出现编号,如: 安尼什3 曼尼什4 echo-n“输入文件名:” 读取文件 在阅读-r-a word时,如果您可以使用更强大的shell,如bash,则可以执行以下操作: $ declare -A words # an associative array, not POSIX $ while read word; do let "words[$word]++"; done <<END anish manish a
echo-n“输入文件名:”
读取文件
在阅读-r-a word时,如果您可以使用更强大的shell,如bash,则可以执行以下操作:
$ declare -A words # an associative array, not POSIX
$ while read word; do let "words[$word]++"; done <<END
anish
manish
anish
manish
anish
END
$ for key in "${!words[@]}"; do echo "$key ${words[$key]}"; done
manish 2
anish 3
$declare-A words#一个关联数组,而不是POSIX
边读边读;让“单词[$word]++”;在不使用命令的情况下完成。。。。。。。酷!你说的“命令”到底是什么意思?如果您指的是“外部程序”,那么我认为您不应该使用[$#-ne 1]
。虽然在某些shell中,[
确实是一个内置命令,但在Posix shell中它是一个外部命令,从您的标记中,我得出结论,您正在寻找符合Posix的解决方案。让我们忘记[$#-ne 1]这里不需要它,那么我应该怎么做?
$ declare -A words # an associative array, not POSIX
$ while read word; do let "words[$word]++"; done <<END
anish
manish
anish
manish
anish
END
$ for key in "${!words[@]}"; do echo "$key ${words[$key]}"; done
manish 2
anish 3