Skip to content

Instantly share code, notes, and snippets.

@13Cubed 13Cubed/checklog.py
Created Feb 20, 2016

Embed
What would you like to do?
Use RegEx (Regular Expressions) to search through files for specific text.
#!/usr/bin/python
import sys
import re
def ParseLog(filename, search_string):
try:
f = open(filename, 'rU')
except IOError:
print '\n*** I/O Error: Can\'t read file', filename, '***\n'
return
lines = f.readlines()
for line in lines:
result = re.search(search_string, line)
if result:
print line,
f.close()
return
def main():
if len(sys.argv) < 2:
print 'usage: ./checklog filename1 filename2 filename3 ...'
sys.exit(0)
args = sys.argv
args.remove(sys.argv[0])
search_string = raw_input('\nPlease enter RegEx search string: ')
for arg in args:
ParseLog(arg, search_string)
print '\nCopyright (C) 2011 13Cubed. All rights reserved.'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.