Last active
December 20, 2015 23:09
-
-
Save YoukouTenhouin/6210106 to your computer and use it in GitHub Desktop.
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 | |
# 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