How to create a frequency list of every word in a file? How to create a frequency list of every word in a file? bash bash

How to create a frequency list of every word in a file?


Not sed and grep, but tr, sort, uniq, and awk:

% (tr ' ' '\n' | sort | uniq -c | awk '{print $2"@"$1}') <<EOFThis is a file with many words.Some of the words appear more than once.Some of the words only appear one time.EOFa@1appear@2file@1is@1many@1more@1of@2once.@1one@1only@1Some@2than@1the@2This@1time.@1with@1words@2words.@1


uniq -c already does what you want, just sort the input:

echo 'a s d s d a s d s a a d d s a s d d s a' | tr ' ' '\n' | sort | uniq -c

output:

  6 a  7 d  7 s


You can use tr for this, just run

tr ' ' '\12' <NAME_OF_FILE| sort | uniq -c | sort -nr > result.txt

Sample Output for a text file of city names:

3026 Toronto2006 Montréal1117 Edmonton1048 Calgary905 Ottawa724 Winnipeg673 Vancouver495 Brampton489 Mississauga482 London467 Hamilton