Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79078

Re: Need help in pulling SQL query out of log file...

Newsgroups comp.lang.python
Date 2014-10-13 23:09 -0700
References <6200437a-b90a-4b60-b287-bd5b7fe37e9d@googlegroups.com> <m1icv8$auh$1@dont-email.me>
Message-ID <1bdfe07b-45f4-4549-959e-3eb176a8b055@googlegroups.com> (permalink)
Subject Re: Need help in pulling SQL query out of log file...
From Sagar Deshmukh <sagarddd@gmail.com>

Show all headers | View raw


On Monday, 13 October 2014 22:40:07 UTC-7, alex23  wrote:
> On 14/10/2014 11:47 AM, Sagar Deshmukh wrote:
> 
> > I have a log file which has lot of information like..SQL query.. number of records read...records loaded etc..
> 
> > My requirement is i would like to read the SQL query completly and write it to another txt file..
> 
> 
> 
> Generally we encourage people to post what they've tried to the list. It 
> 
> helps us identify what you know and what you need help with.
> 
> 
> 
> However, given:
> 
> 
> 
>  > the log file may not be always same so can not make static choices...
> 
> 
> 
> You'll probably want to use regular expressions:
> 
> 
> 
>      https://docs.python.org/howto/regex.html
> 
> 
> 
> Regexps let you search through the text for known patterns and extract 
> 
> any that match. To extract all SQL query sections, you'll need to come 
> 
> up with a way of uniquely identifying them from all other sections. 
> 
> Looking at your example log file, it looks like they're all of the format:
> 
> 
> 
>      SQL Query [<the actual sql query>]
> 
> 
> 
>  From that we can determine that all SQL queries are prefixed by 'SQL 
> 
> Query [' and suffixed by ']', so the content you want is everything 
> 
> between those markers. So a possible regular expression might be:
> 
> 
> 
>      SQL Query \[(.*?)\]
> 
> 
> 
> To quickly explain this:
> 
> 
> 
>      1. "SQL Query " matches on that string
> 
>      2. Because [] have meaning for regexes, to match on literal 
> 
> brackets you need to escape them via \[ and \]
> 
>      3. ( ) is a group, whats contained in here will be returned
> 
>      4. .* means to grab all matching text
> 
>      5. ? means to do an "ungreedy" grab ie it'll stop at the first \] 
> 
> it encounters.
> 
> 
> 
> Pulling the queries out of your log file should be as simple as:
> 
> 
> 
>      import re
> 
> 
> 
>      log = open('logfile').read()
> 
>      queries = re.findall("SQL Query \[(.*?)\]", log, re.DOTALL)
> 
> 
> 
> Because the queries can fall across multiple lines, the re.DOTALL flag 
> 
> is required to treat EOL markers as characters.
> 
> 
> 
> Hope this helps.

Hi,

This helps and its working... Thank you so much....

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Need help in pulling SQL query out of log file... Sagar Deshmukh <sagarddd@gmail.com> - 2014-10-13 18:47 -0700
  Re: Need help in pulling SQL query out of log file... alex23 <wuwei23@gmail.com> - 2014-10-14 15:39 +1000
    Re: Need help in pulling SQL query out of log file... Sagar Deshmukh <sagarddd@gmail.com> - 2014-10-13 23:09 -0700

csiph-web