Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 708
SquatLicense Avatar asked Sep 10 '25 03:09

SquatLicense


1 Answers

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
like image 73
RavinderSingh13 Avatar answered Sep 12 '25 16:09

RavinderSingh13