Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch duplicate entries in text file in linux [duplicate]

Tags:

linux

grep

Text file:

 1 1 2 2 3 3 1 1 

I want to catch 1 1 as duplicated

like image 777
Kalin Borisov Avatar asked Oct 05 '12 09:10

Kalin Borisov


People also ask

How do I find duplicates in a text file?

To start your duplicate search, go to File -> Find Duplicates or click the Find Duplicates button on the main toolbar. The Find Duplicates dialog will open, as shown below. The Find Duplicates dialog is intuitive and easy to use. The first step is to specify which folders should be searched for duplicates.


2 Answers

Your question is not quite clear, but you can filter out duplicate lines with uniq:

sort file.txt | uniq 

or simply

sort -u file.txt 

(thanks RobEarl)

You can also print only repeating lines with

sort file.txt | uniq -d 
like image 160
Lev Levitsky Avatar answered Nov 10 '22 00:11

Lev Levitsky


One way using GNU awk:

awk 'array[$0]++' file.txt  

Results:

1 1 
like image 27
Steve Avatar answered Nov 09 '22 23:11

Steve