Created
January 30, 2022 10:22
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
# | |
# Filter input to remove content matching regexp, then output to stdout. | |
# | |
# Example: | |
# cat data.sql | python3 re-filter.py "CREATE TRIGGER.*?;" | |
# | |
import re | |
import sys | |
matcher = re.compile(sys.argv[1], re.MULTILINE | re.DOTALL | re.IGNORECASE) | |
positions = [0] | |
input_str = sys.stdin.read() | |
for m in matcher.finditer(input_str): | |
positions.append(m.start()) | |
positions.append(m.end()) | |
positions.append(len(input_str)) | |
while positions: | |
start = positions.pop(0) | |
end = positions.pop(0) | |
sys.stdout.write(input_str[start:end]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment