Not a homework project, I hope? ;-) The tricky part is that you don't want to count the "L" in Meller twice, right? Hence the "distinct".
$cat t
my nice name is Mike Meller
And then a pipeline of commands to do the transforming:
$tr '[a-z]' '[A-Z]' < t | # Convert all to upper case
fold -b -w 1 | # Break into one letter per line
awk -f t.awk | # Pipe the whole mess to awk to count
sort -r -n # Sort in reverse numeric order
The awk script is best broken into a separate file, though you can just put it all into a bash one-liner:
$cat t.awk
/ / { # Match spaces,
for (c in wc) {dc[c]+=1} # Accumulate word count (wc) into doc count (dc)
split("",wc) # Reset the word count
}
!/ / { # Match non-spaces,
if (wc[$1] == "") wc[$1]=1 # If haven't already seen char in this word, mark it Donny
}
# Finally, output the count and the letter
END {
for (c in wc) {dc[c]+=1} # Accumulate one last time, in case there is no trailing space
for (c in dc) {print c, dc[c]}
}
Which produces (for me) this output:
$tr '[a-z]' '[A-Z]' < t | fold -b -w 1 | awk -f t.awk | sort -r -n
4 M
4 E
3 I
2 N
1 Y
1 S
1 R
1 L
1 K
1 C
1 A