Q:

Find the most popular 5 words with bash command

I need to find the 5 most popular words in a random text, with a bash command. I was trying to make different combinations between cat, wc, uniq, sort and tail, but couldn't get to anything. Either my output would be a single word, or the text itself, or the wordcount of the text... I should be missing smth...
Is it even possible with a one-liner? Should I approach the problem with smth "a level up", like for-do?
F.e. as input: this random text (I've got other texts for my data, but they're too large to show here), and output... well, the 5 most used words (I don't know which ones they are currently :D ).

A:

Does awk count?
BEGIN {
    RS=FS                                  # separate words on separate records
    PROCINFO["sorted_in"]="@val_num_desc"  # set sort order to pick most frequent
} 
{
    gsub(/[^a-zA-Z]/,"")                   # remove non-letters
    a[tolower($0)]++                       # convert to lower case and count words
} 
END {
    for(i in a) {                          # go thru array in frequency order
        print i,a[i]                       # print
        if(++j==5); exit                   # exit in five
    }
}
$ awk -f this.awk lorem.txt
vitae 10
in 10
quis 9
eget 9
nunc 9

A:

$ cat lorem.txt | tr \  '\n' | tr -c -d '[:alpha:]\n' | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -nr | head -5
     10 vitae
     10 in
      9 quis
      9 nunc
      9 eget

Walk-thru:

tr \  '\n' separate words on separate records
tr -c -d '[:alpha:]\n' remove non-letters
tr '[:upper:]' '[:lower:]' convert to lower case
sort | uniq -c |sort -nr sort, count and print in frequency order
|head -5 exit in five

