4

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!

1
  • 2
    Why don't you simply sort original.list | uniq -c? Commented Jun 17, 2014 at 3:06

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

2 Comments

Actually this doesn't seem to work. I get counts, but still some names are listed multiple times with different counts, as if it didn't count every instance. Perhaps this is a product of the large file length?
Are you sure there are no spaces in those ? (eg "abc " != "abc")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.