Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer 1

up vote 22 down vote accepted

You can achieve this with the sort and uniq utilities.

example:

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

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

share|improve this answer
3  
@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
4  
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
add comment

Your Answer

 
discard

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.