Last active
August 29, 2015 13:58
-
-
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.
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/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