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?
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- JQ: Select when attribute value exists in a bash a
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
A solution with
sed
,sort
anduniq
:This counts all characters, not only letters. You can filter out with:
If you want to consider uppercase and lowercase as same, just add a translation:
Here is a suggestion:
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:
or to avoid the visual split on the screen, insert a literal newline by type CTRL+V CTRL+J
My solution using
grep
,sort
anduniq
.Ignore case:
Just one awk command
if you want case insensitive, add
tolower()
and if you want only characters,
and if you want only digits, change
/[a-zA-Z]/
to/[0-9]/
if you do not want to show unicode, do
export LC_ALL=C