Calc how many times a word appears in a text file with bash

Calc how many times a word appears in a text file with bash

Problem Description:

So I have a file that’s being generated by another tool. I want to process this file and calculate how many times a word appears in the file. Kinda like a word map to get the tone of the file.

This is what I have so far but for some reason I keep getting just the words and not a count of the words
cat words.txt | tr -s ' ' 'n' | sort | uniq | sort -nr | awk '{print $2 " " $1}'

so for example, if I have test test test in the file, I just get test as my output when in reality I want to see test 3

Solution – 1

When you pass by uniq you are losing the count of each word because uniq strips out the repetition count by default.

You have to use uniq -c which means ‘count’ option.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject