mercredi 6 janvier 2016

Matching lines in files like grep -f

I am trying to find matching lines in two large files, much like grep -f.
Let's say the file f.txt contains 3 lines:

1  
2  
3  

and the file g.txt contains 3 lines:

3  
4  
5  

and I want to print the matching line(s):

3

This is what I have tried:

f=open('f.txt', 'r')  
g=open('g.txt', 'r')  
i=0  
for line in f:  
    search = g.readlines()[i]  
    if line.strip() == search :  
        print line  
    i += 1

This returns the error

'AttributeError: 'file' object has no attribute 'split'

Aucun commentaire:

Enregistrer un commentaire