Skip to content

Instantly share code, notes, and snippets.

@Hosuke
Created October 18, 2023 03:46
Show Gist options
  • Save Hosuke/84defd1915158ade28cae3abd359a406 to your computer and use it in GitHub Desktop.
Save Hosuke/84defd1915158ade28cae3abd359a406 to your computer and use it in GitHub Desktop.
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