Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep

Status
Not open for further replies.

malladisk

Programmer
Jun 14, 2001
69
US
Hi, How can I force grep to return unique results? I mean, if the output has duplicate lines, how do I get grep to print only one line for all these duplicate lines?
Thanks,
Sashi
 
You could pipe the output of grep to uniq....

"your grep command"| uniq

I love deadlines. I like the whooshing sound they make as they fly by - Douglas Adams
 
... but I'm quit sure, you have to sort the output before, to make the work not too complicated for uniq :)

"ygc" | sort | uniq
 
Or pipe it to an awk program

grep ... | awk '{if (!a[$0]){print;a[$0]=1}}'

CaKiwi
 
stefanwagner,

If you are talking about my solution, I believe it will match your example. Did you try it?

CaKiwi
 
Instead of 'grep ere | awk' you can do :
[tt]
awk '/ere/ {if (!a[$0]){print;a[$0]=1}}'
[/tt]

Jean Pierre.
 
CaKiWi: No - I didn't test.
Now I tested - and - surprise: it works.

But of course my solution is much more elegant and shorter - isn't it?
 
stefanwagner,

If sorting the data is acceptable, your solution is good, although

grep ... | sort -u

is slightly simpler. If the data must be kept in its original order, then a solution like mine is necessary. Of course, aigles' solution is better.



CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top