]> code.delx.au - monosys/blob - hacks/find-in-file
Rename all the things
[monosys] / hacks / find-in-file
1 #!/usr/bin/env python2
2
3 import sys
4
5 try:
6 needle = sys.argv[1]
7 haystack = sys.argv[2]
8 except IndexError:
9 print >>sys.stderr, "Usage: %s needle haystack" % sys.argv[0]
10 sys.exit(1)
11
12
13 f = open(needle)
14 magic = f.read(1024)
15 f.close()
16
17 chunk_size = 32768
18 f = open(haystack)
19 count = 0
20 buf = ""
21 while True:
22 newbuf = f.read(chunk_size)
23 if not newbuf:
24 break
25 buf += newbuf
26 pos = buf.find(magic)
27 if pos >= 0:
28 print "found", count + pos
29 count += len(buf) - len(magic)
30 buf = buf[-len(magic):]
31 f.close()
32