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
heemayl
  • 93,925
JalT
  • 163

1 Answers1

3

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
heemayl
  • 93,925