I need a program to count the number of words along with number of occurrences of each word in a file.

(Most of the examples I found only count the total number of words.)

  • At least tell us what OS you're looking at. – random Oct 24 '09 at 10:01
  • Most of what code? – innaM Oct 24 '09 at 10:08
  • i'm using ubuntu os. looking for count of each word in a file. for example critic- 29 the-52 like wise. – ArK Oct 24 '09 at 10:25

The free online service Wordcounter ranks the most frequently used words in any given body of text. Use this to see what words you overuse or maybe just to find some keywords from a document.


If using the command line (or a script) is fine, then there's a million options. Like by using Perl (for Windows you would need to install ActivePerl or Cygwin with Perl):

perl -0777 -lape's/\s+/\n/g' FILENAME | sort | uniq -c | sort -nr


  • perl -0777 causes Perl to operate on the entire file as if it were one line ("slurp mode").
  • 's/\s+/\n/g' changes all whitespace into newlines, thus: the output of Perl is one word per line.
  • The sort command sorts the words alphabetically.
  • The uniq command eliminates duplicate words; with the -c option it also prints the number of occurrences.
  • The second sort command, with the -nr option, sorts the resulting file numerically in descending order.
  • Why is perl needed here? What was wrong with cat FILENAME | sed s/' '/\\n/g ? – Sirex Jan 7 '11 at 16:01
  • Because your example does not sort or give the number of occurrences of the word, while Arjan's does. – AndrewKS Jan 7 '11 at 16:06
  • 2
    @Sirex, I guess you're right: cat FILENAME | sed s/' '/\\n/g | sort | uniq -c | sort -nr might give the same results. – Arjan Jan 8 '11 at 0:43
  • 1
    yea, i was implying that the rest of the command would still be needed. Down with perl one liners ! :) – Sirex Jan 12 '11 at 9:02
  • @Sirex, I am no expert. But why is sed better than perl? (And maybe post this as an answer?) – Arjan Jan 12 '11 at 10:39

This is very easy. Just use the following command:

sort file.txt | uniq -c | sort -r

  • Correct (though I guess sort -r needs the -n flag too), but only if each word is on a single line in the file itself. Indeed the question is not clear on that. – Arjan Jan 8 '11 at 0:40

From a Linux command line:

sed s/' '/\\n/g FILENAME | sort | uniq -c | sort -nr

On Mac OS X, which uses the BSD version of sed, the \n needs some magic:

sed 's/ /\'$'\n/g' FILENAME | sort | uniq -c | sort -nr 
  • I think stack exchange swallowed one of my backslashes. Try that. – Sirex Aug 22 '12 at 20:46
  • Hmm, works for me ! – Sirex Aug 22 '12 at 20:47
  • Ah, the \n might need some magic on a Mac: sed 's/ /\'$'\n/g' FILENAME | sort | uniq -c | sort -nr seems to do the trick in BSD's sed as used on OS X. – Arjan Aug 22 '12 at 20:56
  • (Using 4 spaces to indent code makes life much easier!) – Arjan Aug 22 '12 at 21:12

Linux (Ubuntu) command line:

tr -cs "[:alpha:]" "[\n*]" < file1 | sort | uniq -c | sort -nr > file2

Both MS Office and OpenOffice have this feature. Go to tools/word count.


If you were using Windows, you could use Atlantis word processor. It's got loads of great little features and touches including one to hi-light the overuse of words.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.