Count occurences of a character in a file by sorting results

Hello,

I try to sort results of occurences in an array by using awk but I can't find the right command. that's why I'm asking your help ! :slight_smile:

Please see below the command that I run:

awk '{ for ( i=1; i<=length; i++ ) arr[substr($0, i, 1)]++ }END{ for ( i in arr ) { print i, arr } }' dictionnary.txt

Then, I get this result:

13 - A
3 - B
2 - C
3 - D
6 - E
1 - G
2 - H
8 - I
....

But I want this result:

13 - A
8 - I
6 - E
3 - B
3 - D
2 - C
2 - H
1 - G
.....

Can you please help me.... :frowning:

Please use code tags as required by forum rules!

Pipe result to a sorting step. Try (untested):

awk '{ for ( i=1; i<=length; i++ ) arr[substr($0, i, 1)]++ }END{ for ( i in arr ) { print i, arr } }' dictionnary.txt | sort -gr

If the field separator needs to be "-", add the -t"-" option.

try

awk '{ for ( i=1; i<=length; i++ ) arr[substr($0, i, 1)]++ }END{ for ( i in arr ) { print arr,i } }'  dictionnary.txt | sort -n -r

Thank you very much RudiC and Makarand Dodmis ! :slight_smile:

Solutions from both work very well !