Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you should probably use regular expressions (also called regex) for that. Python has the re module which implements regex for python.</p> <p>See this as an example for the direction to look at: <a href="https://stackoverflow.com/questions/924127/python-regex-finding-multiple-matches-in-a-string">stackoverflow question finding multiple matches in a string</a>.</p> <p>Excerpt from the above: Logfile looks like:</p> <pre><code>[1242248375] SERVICE ALERT: myhostname.com;DNS: Recursive;CRITICAL </code></pre> <p>regex looks like:</p> <pre><code>regexp = re.compile(r'\[(\d+)\] SERVICE NOTIFICATION: (.+)') </code></pre> <p>which goes like this:</p> <ul> <li>r => raw string (alway recommended in regexes)</li> <li>\[ => matches the square bracket (which would be a special character otherwise)</li> <li>(\d+) => matches one ore more decimals \d = decimals and the + for 1 or more</li> <li>\] => followed by a closing square bracket </li> <li>SERVICE NOTIFICATION: => matches exactly these characters in sequence.</li> <li>(.+) => the . (dot) matches any character. And again the + means 1 or more </li> </ul> <p>Parantheses group the results.</p> <p>I made a short regex to start with your logfile format. Assuming your log from above is saved as log.txt. </p> <pre><code>import re regexp = re.compile(r'\[(\d{2}:\d{2}:\d{2}\.xxx\d{3})\][\s]+status[\s]+XYZ[\s]+ID:([0-9A-Zx]+)(.+)') f = open("log.txt", "r") for line in f.readlines(): print line m = re.match(regexp, line) #print m if m: print m.groups() </code></pre> <p>Regexes are not that easy looking or straightforward at first glance but if you search for regex or re AND python you will find helpful examples.</p> <p>Outpus this for me:</p> <pre><code>[13:40:19.xxx021] status XYZ ID:22P00935xxx -4 3.92 quote: 0.98/ 1.02 avg: -0.98 -0.16 ('13:40:19.xxx021', '22P00935xxx', ' -4 3.92 quote: 0.98/ 1.02 avg: -0.98 -0.16') [13:40:19.xxx024] status XYZ ID:22C0099xxx0 -2 26.4 quote: 11.60/ 11.85 avg: -13.20 2.70 ('13:40:19.xxx024', '22C0099xxx0', ' -2 26.4 quote: 11.60/ 11.85 avg: -13.20 2.70') [13:40:19.xxx027] status XYZ ID:22P0099xxx0 10 -17.18 quote: 1.86/ 1.90 avg: -1.72 1.42 ('13:40:19.xxx027', '22P0099xxx0', ' 10 -17.18 quote: 1.86/ 1.90 avg: -1.72 1.42') [13:40:19.xxx029] status XYZ ID:22C00995xxx 4 -42.5 quote: 8.20/ 8.30 avg: -10.62 -9.70 ('13:40:19.xxx029', '22C00995xxx', ' 4 -42.5 quote: 8.20/ 8.30 avg: -10.62 -9.70') [13:40:19.xxx031] status XYZ ID:22P00995xxx 2 9.66 quote: 3.30/ 3.40 avg: 4.83 16.26 ('13:40:19.xxx031', '22P00995xxx', ' 2 9.66 quote: 3.30/ 3.40 avg: 4.83 16.26') </code></pre> <p>Every second line is the output which is a list containing the matched groups.</p> <p>If you add this to the programm above:</p> <pre><code>print "ID is : ", m.groups()[1] </code></pre> <p>the output is:</p> <pre><code>[13:40:19.xxx021] status XYZ ID:22P00935xxx -4 3.92 quote: 0.98/ 1.02 avg: -0.98 -0.16 ID is : 22P00935xxx [13:40:19.xxx024] status XYZ ID:22C0099xxx0 -2 26.4 quote: 11.60/ 11.85 avg: -13.20 2.70 ID is : 22C0099xxx0 </code></pre> <p>Which matches your IDs you want to compare. Just play with it a little to get the result you really want.</p> <p><strong>Final example</strong> catches the ID, tests if its already there and adds the matched lines to a dictionary which has te IDs as its key:</p> <p>import re regexp = re.compile(r'[(\d{2}:\d{2}:\d{2}.xxx\d{3})][\s]+status[\s]+XYZ[\s]+ID:([0-9A-Zx]+)(.+)')</p> <pre><code>res = {} f = open("log.txt", "r") for line in f.readlines(): print line m = re.match(regexp, line) if m: print m.groups() id = m.groups()[1] if id in res: #print "added to existing ID" res[id].append([m.groups()[0], m.groups()[2]]) else: #print "new ID" res[id] = [m.groups()[0], m.groups()[2]] for id in res: print "ID: ", id print res[id] </code></pre> <p>Now you can play around and fine tune it to adapt it to your needs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload