Bash script to find the frequency of every letter in a file

前端 未结 5 2119
眼角桃花
眼角桃花 2021-02-06 23:26

I am trying to find out the frequency of appearance of every letter in the english alphabet in an input file. How can I do this in a bash script?

5条回答
  •  长情又很酷
    2021-02-07 00:26

    A solution with sed, sort and uniq:

    sed 's/\(.\)/\1\n/g' file | sort | uniq -c
    

    This counts all characters, not only letters. You can filter out with:

    sed 's/\(.\)/\1\n/g' file | grep '[A-Za-z]' | sort | uniq -c
    

    If you want to consider uppercase and lowercase as same, just add a translation:

    sed 's/\(.\)/\1\n/g' file | tr '[:upper:]' '[:lower:]' | grep '[a-z]' | sort | uniq -c
    

提交回复
热议问题