[Tutor] Log file for to search in order

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jan 11 18:58:24 EST 2019


On 11/01/2019 14:12, Asad wrote:

Let metry and translate your description into pseudo code...

>     >> Anywhere forward from the line which matches the search
> 
> 
>> You are looking for two patterns, but does the order they appear, or the
>> distance apart, matter?
>>
>>      Yes the order does matter :
> 
>   1)      first it looks for :  \?/patch/\d{8}/\d{8}/admin/load.sql
>            - Condition if there are 10 lines containing the same pattern
> then it should save and print the last line containing the pattern means
> the 10th line
>          - if it find the above string it start searching from that line
> with successful match regex match until it gets to the line containing
> set_metadata
>            - Once it get the line containing set_metadata print last 4
> lines prior to the line containing  set_metadata

start = False
lines = []
with open(fname) as infile:
    for line in infile:
        lines.append(line)    # or use a circular list size 4
        if hasPattern(patt1,line):
           start = True
           break
        if start and hasPattern(patt2, line):
           print lines[-4:]

Is that right so far?


>  2)       if it doesnot find  the above pattern look for second in order
>                        :Starting\s+apply\s+for\s+patch\s+\d{8}/\d{8}
>           - Condition if there are 10 lines containing the same pattern
> then it should save and print the last line containing the pattern means
> the 10th line
>            -  if it find the above string it start searching from that line
> with successful match regex match until it gets to the line containing
> set_metadata
>             - Once it get the line containing set_metadata print last 10
> lines prior to the line containing  set_metadata

This sounds like the same thing except with a different start pattern
and a different number of lines printed.
So may be its a function?

The only thing I'm not clear on is the comment "if it does not find the
above pattern"
Which pattern, there are two... do you mean the first one?

And if so do you need to search the entire file for the second pattern?
If so you need to repeat the search fuinction twice:

found = False
with open(fname) as infile:
   found = searchFile(infile, pattern1, buffsize=4)
   if not found:
      infile.seek(0)   # go back to beginning of file
      found = searchFile(infile,pattern2, buffsize=10)

> 3)  if it doesnot match  the pattern: <pattenl>
> or <pattern2>  anywhere in the log file .
>
> print "No match found refer to install guide"

   if not found:
      print ....


Does the above look like what you want?

> 4) If I have two errors how do I prioritize one above other ?

I have no idea. How do you prioritize them?
That's a requirement issue not a solution issue.

Once you tell us how you want to prioritize them we can
consider possible programming options.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list