0

I have a file which contain following content.

Hi
abcd
Hi
abc
hello
hello
xyz
hello

I want the find out duplicate lines as well as howmany times it is repeteing.My expected output is as below.

2 Hi
3 hello

I have used following command alreay which give me duplicate line come one after the other (i.e Hello Hello it works but Hello hi Hello it didn't work)

uniq -d filename
2

You need to sort the input file first before passing to uniq to make same lines sequential/adjacent:

sort file.txt | uniq -dc

-c will count the number of occurrences of repeated lines.

Example:

$ sort file.txt | uniq -dc
3 hello
2 Hi

$ sort file.txt | uniq -dc | sort -k1,1n  ## Your expected output
2 Hi
3 hello

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.