]> code.delx.au - monosys/blobdiff - hacks/find-in-file
Rename all the things
[monosys] / hacks / find-in-file
diff --git a/hacks/find-in-file b/hacks/find-in-file
new file mode 100755 (executable)
index 0000000..12ee05e
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/env python2
+
+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()
+