Last active
July 30, 2018 16:14
-
-
Save yarick/eb3d5645cb854a1a268f90a334c2348f to your computer and use it in GitHub Desktop.
Fast Search for text or pattern in memory mapped file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 | |
import re, mmap, os, contextlib, sys | |
#print "Usage: ./s1.py file regex(to print)" | |
# print "Example: ./s1.py /tmp/a.log 10.213.194.\S+:http://([^/]+)/" | |
myString = str(sys.argv[2]) | |
print "Searching file: " , str(sys.argv[1]) | |
fn = sys.argv[1] | |
size = os.stat(fn).st_size | |
re.compile(myString) | |
with open(fn, 'r+') as f: | |
data = mmap.mmap(f.fileno(), size) | |
for match in re.finditer(myString, data, re.S): | |
print match.group(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment