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)
In GNU awk
with your shown samples only, please try following awk
code. Where I am setting RS
to (^|\n)OPEN\\(\n,*[^\n]*\n[^)]*\\) *CLOSE
as per requirement and then playing around with RT to get the required output.
Here is the Online Demo for used regex for understanding purposes, note awk
we need to double escape but in regex site single escape works, so that's why regex looks little different here.
awk -v RS='(^|\n)OPEN\\(\n,*[^\n]*\n[^)]*\\) *CLOSE' '
RT{
sub(/^\n/,"",RT)
sub(/,/,"",RT)
print RT
}
' Input_file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With