Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding duplicates in a field and printing them in unix bash

Tags:

bash

unix

awk

I have a file the contains

apple
apple
banana
orange
apple
orange

I want a script that finds the duplicates apple and orange and tells the user that the following : apple and orange are repeated. I tried

nawk '!x[$1]++' FS="," filename

to find repeated item so how can i print them out in unix bash ?

like image 776
t28292 Avatar asked Jul 29 '13 06:07

t28292


1 Answers

In order to print the duplicate lines, you can say:

$ sort filename | uniq -d
apple
orange

If you want to print the count as well, supply the -c option to uniq:

$ sort filename | uniq -dc
      3 apple
      2 orange
like image 137
devnull Avatar answered Sep 23 '22 23:09

devnull