Created
April 16, 2013 12:30
-
-
Save perliedman/5395526 to your computer and use it in GitHub Desktop.
Simple utility to grep/find a byte pattern in a file. I can only assume you can use grep or some other utility to do this as well.
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/python | |
from sys import argv | |
path = argv[1] | |
hex_pattern = argv[2] | |
str_pattern = "".join([chr(int(hex_pattern[x:x+2], 16)) for x in range(0,len(hex_pattern), 2)]) | |
with open(path, 'r') as f: | |
data = f.read() | |
pos = data.find(str_pattern) | |
if pos >= 0: | |
while pos >= 0: | |
start = max(0, pos - 16) | |
end = min(len(data), pos + len(str_pattern) + 16) | |
print "Found at position %d. Showing characters %d-%d" % (pos, start, end) | |
print data[start:end] | |
print "".join([" " for x in xrange(start, pos)]) + '^' | |
pos = data.find(str_pattern, pos + 1) | |
else: | |
print "Not found." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment