Skip to content

Instantly share code, notes, and snippets.

@YoukouTenhouin
Last active December 20, 2015 23:09
Show Gist options
  • Save YoukouTenhouin/6210106 to your computer and use it in GitHub Desktop.
Save YoukouTenhouin/6210106 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Simple grep
# usage: simple_grep.py <regex> <filename>
import sys
import re
if __name__ == "__main__":
try:
regex = sys.argv[1]
filename = sys.argv[2]
with open(filename) as f:
cont = f.read()
except IndexError:
print('usage: simple_grep.py <regex> <filename>')
except FileNotFoundError:
print('Cannot open file %s'%filename)
except UnicodeDecodeError:
print('Is this file a binary file?')
else:
lines = cont.split('\n')
rex = re.compile(regex)
for i in lines:
if rex.search(i):
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment