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

前端 未结 5 2117
眼角桃花
眼角桃花 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:07

    Similar to mouviciel's answer above, but more generic for Bourne and Korn shells used on BSD systems, when you don't have GNU sed, which supports \n in a replacement, you can backslash escape a newline:

    sed -e's/./&\
    /g' file | sort | uniq -c | sort -nr
    

    or to avoid the visual split on the screen, insert a literal newline by type CTRL+V CTRL+J

    sed -e's/./&\^J/g' file | sort | uniq -c | sort -nr
    

提交回复
热议问题