Created
January 29, 2018 15:26
-
-
Save ij96/10095dcbf9c4007aaabe672bff89a044 to your computer and use it in GitHub Desktop.
Extracts all regex matches in a file and output to another file
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/python3.5 | |
| import re, sys | |
| SEPARATOR = '\n' | |
| def extract(pattern, in_file_name, out_file_name): | |
| in_file_content = "" | |
| with open(in_file_name, 'r') as in_file: | |
| in_file_content = in_file.read() | |
| matches = re.findall(pattern, in_file_content) | |
| with open(out_file_name, 'w') as out_file: | |
| out_file.write(SEPARATOR.join(matches)) | |
| pattern = r"regex expression here" | |
| extract(pattern, sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment