Operating System - HP-UX
1831478 Members
3285 Online
110025 Solutions
New Discussion

how to output unique line from a file/input

 
SOLVED
Go to solution
Xiaoming Zhang
Contributor

how to output unique line from a file/input

I used grep to get lines matching to a regular expression. I also like to filter the grep's output to get unique lines. How may I do it?

Thanks. Xiaoming
5 REPLIES 5
Curtis Larson
Trusted Contributor
Solution

Re: how to output unique line from a file/input

you can use sort -u or uniq
MARTINACHE
Respected Contributor

Re: how to output unique line from a file/input

Hi,

grep your_pattern your_file | sort -u

Regards,

Patrice.
Patrice MARTINACHE
Paula J Frazer-Campbell
Honored Contributor

Re: how to output unique line from a file/input

Hi
Your can pipe your grep output ro grep again,

cat |grep |grep |etc

Build it up stage by stage to get what you require.

HTH

Paula
If you can spell SysAdmin then you is one - anon
Dan Hetzel
Honored Contributor

Re: how to output unique line from a file/input

Hi Xiaoming,

As 'uniq' looks for adjacent lines, the file needs to be sorted before.
For that reason, it's better to use 'sort -u' instead.

grep "pattern" from_file | sort -u > to_file

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Bill McNAMARA_1
Honored Contributor

Re: how to output unique line from a file/input

I know it's old, I was searching for something else and this showed up..

one of the best ways to do this is the uniq command.
| sort | uniq -c in particular may be what's needed here.

Later,
Bill
It works for me (tm)