1

I have a bunch of stored procedure scripts from sql server that I'd like to extract the custom coding section from. The area I'd like to grab is preceded by this:

/* **************************************************************************************************************************************************** */
    /* * Begin Custom Code ******************************************************************************************************************************** */
    /* **************************************************************************************************************************************************** */

and followed by this:

/* **************************************************************************************************************************************************** */
    /* * End Custom Code ********************************************************************************************************************************** */
    /* **************************************************************************************************************************************************** */

So the script would look like:

/* **************************************************************************************************************************************************** */
    /* * Begin Custom Code ******************************************************************************************************************************** */
    /* **************************************************************************************************************************************************** */

    SELECT col1, col2
    FROM [dbo].[table1]
    ORDER BY col1

    /* **************************************************************************************************************************************************** */
    /* * End Custom Code ********************************************************************************************************************************** */
    /* **************************************************************************************************************************************************** */

Is there a way to read the file and write the lines in between those markers to a separate file as one long string (no tabs or line breaks)?

screechOwl
  • 27,310
  • 61
  • 158
  • 267

3 Answers3

0

Assuming you can read the text in yourself, you might do something like this:

(Inspired by this answer)

chunks = sql.replace('/*', '*/').split('*/')
relevant = [x for x in chunks if not x.startswith('/*')
Community
  • 1
  • 1
LondonRob
  • 73,083
  • 37
  • 144
  • 201
  • Oops. My split on multiple conditions doesn't work, I don't think. If someone can fix it, this answer will make sense! – LondonRob Jun 26 '15 at 14:51
0

Here is the basic idea

inputArray = inputFile.split('*/')
for val in inputArray:
    if not val.strip().startswith('/*'):
        print(val.strip().split("\n").join(" "))
dstudeba
  • 8,878
  • 3
  • 32
  • 41
  • HAHA! Sorry I have been working on PHP recently, I was just fixing it after I realized my mistake. You caught me first! – dstudeba Jun 26 '15 at 14:58
0

Read each line and do strip on it. If the resulting line is empty or starts with '/*' skip and read next line. Ohterwise append to a list.

sql = '''/* **************************************************************************************************************************************************** */
    /* * Begin Custom Code ******************************************************************************************************************************** */
    /* **************************************************************************************************************************************************** */

    SELECT col1, col2
    FROM [dbo].[table1]
    ORDER BY col1

    /* **************************************************************************************************************************************************** */
    /* * End Custom Code ********************************************************************************************************************************** */
    /* **************************************************************************************************************************************************** */
'''
lines = []
for line in sql.splitlines():
    line = line.strip()
    if not line or line.startswith('/*'):
        continue
    lines.append(line)

' '.join(lines)

'SELECT col1, col2 FROM [dbo].[table1] ORDER BY col1'  

Using regex;

' '.join(line.strip() for line in re.sub(r'/\*.*?\*/','',sql,flags=re.DOTALL).strip().splitlines())  

'SELECT col1, col2 FROM [dbo].[table1] ORDER BY col1'

Nizam Mohamed
  • 8,751
  • 24
  • 32