-5

How to exclude unique lines from output of sort?

Let's say I'm having the following text:

aa
ab
ax    (NOT dupplicated line)
aa
aa
ab
az    (NOT dupplicated line)
ay    (NOT dupplicated line)

and I want to remove non-duplicate lines from it:

aa
aa
aa
ab
ab

How would I do it using sort?

Błażej Michalik
  • 4,474
  • 40
  • 55
  • @BłażejMichalik: Googling for `unix sort keep only duplicates` found two solutions: [one on Stack Exchange](http://unix.stackexchange.com/questions/52534), [another on unix.com](http://www.unix.com/shell-programming-and-scripting/189519-bash-keep-only-duplicate-lines-file.html). Please read up on [how to ask a good question](http://stackoverflow.com/help/how-to-ask). – Tom Zych Nov 16 '15 at 01:55
  • @TomZych Question though, how can it be that this question: http://stackoverflow.com/questions/9377040/remove-duplicate-entries-using-a-bash-script gets approved widely, while mine, being on the same context, and same level of research, gives me links to SO newbie guide? – Błażej Michalik Nov 16 '15 at 02:09
  • @BłażejMichalik: That is a very good question. [I've asked about it on meta](http://meta.stackexchange.com/questions/269474); let's see what people think. – Tom Zych Nov 16 '15 at 10:46

1 Answers1

5

Use sort with uniq:

command | sort | uniq -D
-D, --all-repeated[=delimit-method]

print all duplicate lines delimit-method={none(default),prepend,separate} Delimiting is done with blank lines

choroba
  • 231,213
  • 25
  • 204
  • 289