How to sort file by character occurrences per line

sorttext processing

I'm quite new to Linux, and I've found quite a bit of useful information on how to do character counts in a file, but is there a way in Linux/terminal to sort a text file by the number of times a specific character occurs per line?

E.g. given:

baseball
aardvark
a man a plan a canal panama
cat
bat
bill

Sort by the number of occurrences of the letter "a" yielding:

a man a plan a canal panama
aardvark
baseball
cat
bat
bill

Regarding "cat" and "bat" at one occurrence of "a" each, I don't care if the order of lines with equal counts get reversed, just interested in a general sort of lines by character frequency.

Best Answer

The general approach with this kind of task is to use awk or perl... to compute the metric you're interested in and prepend it to the line, and then feed that to sort and remove the metric off the sorted output:

awk '{print gsub("a","a"), $0}' < file | sort -rn | cut -d' ' -f2-
Related Question