Skip to content

Instantly share code, notes, and snippets.

@ij96
Created January 29, 2018 15:26
Show Gist options
  • Select an option

  • Save ij96/10095dcbf9c4007aaabe672bff89a044 to your computer and use it in GitHub Desktop.

Select an option

Save ij96/10095dcbf9c4007aaabe672bff89a044 to your computer and use it in GitHub Desktop.
Extracts all regex matches in a file and output to another file
#!/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