Home > database >  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

Time:12-07

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

CodePudding user response:

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.

  •  Tags:  
  • bash
  • Related