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?
Asked
Active
Viewed 2.4e+01k times
102
hap497
- 3,039
2 Answers
158
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.
21
You can use:
grep -rohP "(mySearchString)" . | sort -u
-r: recursive
-o: only print matching part of the text
-h: don't print filenames
-P: Perl style regex (you may use -E instead depending on your case)
sort -u is better than sort | uniq, as @Chris Johnsen pointed out.
Pato
- 311
- 2
- 3