Created
October 18, 2023 03:46
-
-
Save Hosuke/84defd1915158ade28cae3abd359a406 to your computer and use it in GitHub Desktop.
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
import os | |
import re | |
# Get the current working directory | |
directory = os.getcwd() | |
# Iterate over all files in the directory and its subdirectories | |
for dirpath, dirnames, filenames in os.walk(directory): | |
for filename in filenames: | |
# Process only files with a .sql extension | |
if filename.endswith('.sql'): | |
filepath = os.path.join(dirpath, filename) | |
# Read the content of the file | |
with open(filepath, 'r') as file: | |
filedata = file.read() | |
# Define the pattern to search for and the text to replace it with | |
pattern = r"\b(\w+\.\w+)\s*>=\s*date_trunc\('day',\s*now\(\)\s*-\s*interval\s*'7'\s*day\)" | |
replacement_text = "{{ incremental_predicate('\\1') }}" | |
# Use regular expression to replace the dates, ignoring case | |
filedata = re.sub(pattern, replacement_text, filedata, flags=re.IGNORECASE) | |
# Write the modified content back to the file | |
with open(filepath, 'w') as file: | |
file.write(filedata) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment