Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In linux, I can grep a string from a file using grep mySearchString myFile.txt. How can I only get the result which are unique?

share|improve this question
up vote 55 down vote accepted

You can achieve this with the sort and uniq utilities.


[john@awesome ~]$ echo -e "test\ntest\ntest\nanother test\ntest"
another test
[john@awesome ~]$ echo -e "test\ntest\ntest\nanother test\ntest" | sort | uniq
another test

depending on the data you may want to utilize some of the switches as well.

share|improve this answer
@John T - I would recommend to use sort before uniq in case the data are not ordered. Otherwise uniq won't completely work. – Studer Feb 21 '10 at 2:58
@Studer forgot about that detail, thank you! – John T Feb 21 '10 at 3:05
t now I can upvote ! You also helped me writing others scripts here ;) – Studer Feb 21 '10 at 3:17
Use sort -u instead of sort | uniq. It saves a process, reduces the total I/O, and reduces the total number of comparisons that have to be made. – Chris Johnsen Feb 22 '10 at 5:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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