Operating System - HP-UX
1826614 Members
3197 Online
109695 Solutions
New Discussion

Re: How to print duplicate lines alone

 
SOLVED
Go to solution
Nesan
Advisor

How to print duplicate lines alone

I want to know how to print duplicate lines alone from a file.
When I tried 'uniq -c', tt prints number of times and line. For eg.

$ uniq -c serrlog
1 error 04: connection failure
4 error 11: /tmp directory not found
1 error 17: low disk space
2 error 22: out of memory

Instead of printing just number of times, I need to have print

I am not sure this option is available on any command on the unix.

Thank you
Regards
Nesan
Everything you don't know is an opportunity to learn
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to print duplicate lines alone

Hi Nesan:

Here's a quick, crude way to do this:

Given your file (sorted, of course):

# uniq -u myfile > myfile.tmp
# diff myfile.tmp myfile|sed -n '/>/p'|sed -e 's/> //g'

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: How to print duplicate lines alone

Hi Nesan

Did you try 'uniq -d' ?

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Darren Prior
Honored Contributor

Re: How to print duplicate lines alone

Hi,

I'm not quite sure what you're after as it looks like you chopped off part of your question ;)

If you want to see all unique lines plus one copy of the duplicates, without the number of duplicates at the front then you should use:
uniq serrlog

This is equivalent to uniq -ud serrlog. The -c option is adding the numbers.

uniq -d serlog will give you one line for each duplicate - without numbers.

regards,

Darren.
Calm down. It's only ones and zeros...
John Palmer
Honored Contributor

Re: How to print duplicate lines alone

I'd just write a small script fragment to process the output from your uniq -c example...

integer N
uniq -c serrlog | {
while read N LINE
do
while (( N > 0 ));
do
print -- "${LINE}"
let N=N-1
done
done
}

Regards,
John