Using python I would like to know how to delete the first occurrence of a character between two strings in a file
For example if I had some file:
OPEN(
,a
,b
) CLOSE
OPEN(
c
) CLOSE
OPEN(
,d
,e
,f
) CLOSE
I would like to remove the first occurrence of the character ',' following each 'OPEN(' but only if it occurs before the next ') CLOSE' such that the resulting file would look like:
OPEN(
a
,b
) CLOSE
OPEN(
c
) CLOSE
OPEN(
d
,e
,f
) CLOSE
Any thoughts on how I should approach? I have tried using regex, but I don't know how to specify conditions. Could some combination of awk & sed be used?
attempted regex solution:
pattern = r'WITH \(([^)]+),([^)]+)\) AS'
replacement = r'WITH (\1\2) AS'
sql_content_modified = re.sub(pattern, replacement, sql_content)
ended up solving with something like:
# Read the SQL file
with open(f'{filename}', 'r') as file:
content = file.read()
content_modified = content.replace('(\n,', '(')
content_modified = re.sub('--<([a-z]*.*[A-Z]*)>', '', content) # removes the --<*> lines
# Write the modified content back to the file
with open(f'{filename}', 'w') as file:
file.write(content_modified)
remove_empty_lines_from_file(filename)
# now do the same but replace "WITH (\n," with "WITH (\n" ...
with open(f'{filename}', 'r') as file:
content = file.read()
content_modified = content.replace('(\n,', '(\n')
with open(f'{filename}', 'w') as file:
file.write(content_modified)
source https://stackoverflow.com/questions/76435215/using-python-i-would-like-to-know-how-to-delete-the-first-occurrence-of-a-charac
Comments
Post a Comment