Skip to content

Instantly share code, notes, and snippets.

@pmbuko
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save pmbuko/10306792 to your computer and use it in GitHub Desktop.

Select an option

Save pmbuko/10306792 to your computer and use it in GitHub Desktop.
This is a python example for an easy way to search file lines for lines matching a specific regex. You need to pass the filename to search as the first argument.
#!/usr/bin/env python
import sys, re
# get first command line argument as filename
input_file = sys.argv[1]
# suck in file contents
with open(input_file) as f:
content = f.readlines()
# strip newline characters
lines = [line.strip() for line in content]
# search through the file, line by line, print matching lines.
for line in lines:
# this regex uses a back-reference. each set of parens is a saved match.
# the '\1' references the first saved match
matchObj = re.match( r'([A-Za-z0-9]+) => (12)?\1$', line)
if matchObj:
print "FOUND MATCH: ", matchObj.group()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment