finding last match in a file

Keith O'Connell keith_oconnell at
Thu Sep 19 19:45:08 CEST 2002

Dag <dag at> writes:

> I have a number of very large files where I have to find the last line
> which matches a certain experssion.  Currently I'm simply opening the
> file and looping through it noteing each time a line matches and at the
> end of the loop I see which was the last match.  However this is very
> slow and inefficient.  Is there some way to read through the file
> backwards and simply find first match starting from the end of the file.
> The files are really too big to read the entire thing into memory.

Would you not be able to use grep with the "-n" option. It will list
all your matches with the lines numbered so you can see the last match
having the highest line number. If you then use wc on the file you will
get the total number of lines. Take one from the other and you can see
how far from the end your last match is.

That seems to easy - Have I misunderstood you question?


___________________________________   _
  Keith O'Connell.                   -o)
  Maidstone, Kent. (UK)              /\\
  keith_oconnell at   _\_v

More information about the Python-list mailing list