Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get frequency counts of unique values in a list using UNIX?

Tags:

bash

unix

I have a file that has a couple thousand domain names in a list. I easily generated a list of just the unique names using the uniq command. Now, I want to go through and find how many times each of the items in the uniques list appears in the original, non-unique list. I thought this should be pretty easy to do with this loop, but I'm running into trouble:

for name in 'cat uniques.list'; do grep -c $name original.list; done > output.file

For some reason, it's spitting out a result that shows some count of something (honestly not sure what) for the uniques file and the original file.

I feel like I'm overlooking something really simple here. Any help is appreciated.

Thanks!

like image 341
user3746901 Avatar asked Oct 19 '25 06:10

user3746901


1 Answers

Simply use uniq -c on your file :

-c, --count prefix lines by the number of occurrences

The command to get the final output :

sort original.list | uniq -c

like image 186
quantdev Avatar answered Oct 21 '25 22:10

quantdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!