]> code.delx.au - monosys/commitdiff
Simple find-in-file tool
authorJames Bunton <jamesbunton@delx.net.au>
Thu, 21 Mar 2013 11:09:11 +0000 (22:09 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Thu, 21 Mar 2013 11:09:11 +0000 (22:09 +1100)
bin/find-in-file [new file with mode: 0755]

diff --git a/bin/find-in-file b/bin/find-in-file
new file mode 100755 (executable)
index 0000000..3f80250
--- /dev/null
@@ -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()
+