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.
tr -d -c 'a\n' <infile | awk '{print length}' | paste - infile | sort -k1nr | cut -f2-
– don_crissti Aug 01 '15 at 20:29