Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find duplicate lines in a text file and print them? [closed]

Tags:

python

text

I have a text file with some 1,200 rows. Some of them are duplicates.

How could I find the duplicate lines in the file (but not worrying about case) and then print out the line's text on the screen, so I can go off and find it? I don't want to delete them or anything, just find which lines they might be.

like image 370
samiles Avatar asked Oct 17 '12 15:10

samiles


2 Answers

This is pretty easy with a set:

with open('file') as f:
    seen = set()
    for line in f:
        line_lower = line.lower()
        if line_lower in seen:
            print(line)
        else:
            seen.add(line_lower)
like image 132
mgilson Avatar answered Oct 05 '22 03:10

mgilson


as there are only 1200 lines, so you can also use collections.Counter():

>>> from collections import Counter

>>> with open('data1.txt') as f:
...     c=Counter(c.strip().lower() for c in f if c.strip()) #for case-insensitive search
...     for line in c:
...         if c[line]>1:
...             print line
... 

if data1.txt is something like this:

ABC
abc
aBc
CAB
caB
bca
BcA
acb

output is:

cab
abc
bca
like image 30
Ashwini Chaudhary Avatar answered Oct 05 '22 02:10

Ashwini Chaudhary