From: James Bunton Date: Thu, 21 Mar 2013 11:09:11 +0000 (+1100) Subject: Simple find-in-file tool X-Git-Url: https://code.delx.au/monosys/commitdiff_plain/3800ad26e35363d4cdd8475b2c7b5eab54882065 Simple find-in-file tool --- diff --git a/bin/find-in-file b/bin/find-in-file new file mode 100755 index 0000000..3f80250 --- /dev/null +++ b/bin/find-in-file @@ -0,0 +1,32 @@ +#!/usr/bin/python + +import sys + +try: + needle = sys.argv[1] + haystack = sys.argv[2] +except IndexError: + print >>sys.stderr, "Usage: %s needle haystack" % sys.argv[0] + sys.exit(1) + + +f = open(needle) +magic = f.read(1024) +f.close() + +chunk_size = 32768 +f = open(haystack) +count = 0 +buf = "" +while True: + newbuf = f.read(chunk_size) + if not newbuf: + break + buf += newbuf + pos = buf.find(magic) + if pos >= 0: + print "found", count + pos + count += len(buf) - len(magic) + buf = buf[-len(magic):] +f.close() +