<p>I am working on a project that requires the parsing of log files. I am looking for a fast algorithm that would take groups messages like this:</p>
<blockquote>
<p>The temperature at P1 is 35F.</p>
<p>The temperature at P1 is 40F.</p>
<p>The temperature at P3 is 35F.</p>
<p>Logger stopped.</p>
<p>Logger started.</p>
<p>The temperature at P1 is 40F.</p>
</blockquote>
<p>and puts out something in the form of a printf():</p>
<pre><code>&quot;The temperature at P%d is %dF.&quot;, Int1, Int2&quot; 
{(1,35), (1, 40), (3, 35), (1,40)}
</code></pre>
<p>The algorithm needs to be generic enough to recognize almost any data load in message groups.</p>
<p>I tried searching for this kind of technology, but I don't even know the correct terms to search for.</p>

[{'answer_id': 9886, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 4, 'selected': False, 'text': '<p>I think you might be overlooking and missed fscanf() and sscanf(). Which are the opposite of fprintf() and sprintf().</p>\n'}, {'answer_id': 9980, 'author': 'Niels', 'author_id': 930, 'author_profile': 'https://Stackoverflow.com/users/930', 'pm_score': 2, 'selected': False, 'text': '<p>It depends on what you are trying to do, if your goal is to quickly generate sprintf() input, this works.  If you are trying to parse data, maybe regular expressions would do too.. </p>\n'}, {'answer_id': 10576, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 0, 'selected': False, 'text': '<p>@John: I think that the question relates to an algorithm that actually recognises patterns in log files and automatically "guesses" appropriate format strings and data for it. The <code>*scanf</code> family can\'t do that on its own, it can only be of help once the patterns have been recognised in the first place.</p>\n'}, {'answer_id': 10589, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 1, 'selected': False, 'text': '<p>You\'re not going to find a tool that can simply take arbitrary input, guess what data you want from it, and produce the output you want.  That sounds like strong AI to me.</p>\n\n<p>Producing something like this, even just to recognize numbers, gets really hairy.  For example is "123.456" one number or two?  How about this "123,456"?  Is "35F" a decimal number and an \'F\' or is it the hex value 0x35F?  You\'re going to have to build something that will parse in the way you need.  You can do this with regular expressions, or you can do it with <code>sscanf</code>, or you can do it some other way, but you\'re going to have to write something custom.</p>\n\n<p>However, with basic regular expressions, you can do this yourself.  It won\'t be magic, but it\'s not that much work.  Something like this will parse the lines you\'re interested in and consolidate them (Perl):</p>\n\n<pre><code>my @vals = ();\nwhile (defined(my $line = &lt;&gt;))\n{\n    if ($line =~ /The temperature at P(\\d*) is (\\d*)F./)\n    {\n        push(@vals, "($1,$2)");\n    }\n}\nprint "The temperature at P%d is %dF. {";\nfor (my $i = 0; $i &lt; @vals; $i++)\n{\n    print $vals[$i];\n    if ($i &lt; @vals - 1)\n    {\n        print ",";\n    }\n}\nprint "}\\n";\n</code></pre>\n\n<p>The output from this isL</p>\n\n<pre><code>The temperature at P%d is %dF. {(1,35),(1,40),(3,35),(1,40)}\n</code></pre>\n\n<p>You could do something similar for each type of line you need to parse.  You could even read these regular expressions from a file, instead of custom coding each one.</p>\n'}, {'answer_id': 12321, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Thanks for all the great suggestions.\nChris, is right. I am looking for a generic solution for normalizing any kind of text. The solution of the problem boils down to dynmamically finding patterns in two or more similar strings. \nAlmost like predicting the next element in a set, based on the previous two:</p>\n\n<p>1: Everest is 30000 feet high</p>\n\n<p>2: K2 is 28000 feet high</p>\n\n<p>=> What is the pattern?\n=> Answer:</p>\n\n<p>[name] is [number] feet high</p>\n\n<p>Now the text file can have millions of lines and thousands of patterns. I would like to parse the files very, very fast, find the patterns and collect the data sets that are associated with each pattern.</p>\n\n<p>I thought about creating some high level semantic hashes to represent the patterns in the message strings.\nI would use a tokenizer and give each of the tokens types a specific "weight". \nThen I would group the hashes and rate their similarity. Once the grouping is done I would collect the data sets.</p>\n\n<p>I was hoping, that I didn\'t have to reinvent the wheel and could reuse something that is already out there.</p>\n\n<p>Klaus</p>\n'}, {'answer_id': 12379, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 1, 'selected': False, 'text': '<p>I don\'t know of any specific tool to do that. What I did when I had a similar problem to solve was trying to guess regular expressions to match lines.</p>\n\n<p>I then processed the files and displayed only the unmatched lines. If a line is unmatched, it means that the pattern is wrong and should be tweaked or another pattern should be added.</p>\n\n<p>After around an hour of work, I succeeded in finding the ~20 patterns to match 10000+ lines.</p>\n\n<p>In your case, you can first "guess" that one pattern is <code>"The temperature at P[1-3] is [0-9]{2}F."</code>. If you reprocess the file removing any matched line, it leaves "only":</p>\n\n<blockquote>\n  <p>Logger stopped. </p>\n  \n  <p>Logger started.</p>\n</blockquote>\n\n<p>Which you can then match with <code>"Logger (.+)."</code>.</p>\n\n<p>You can then refine the patterns and find new ones to match your whole log.</p>\n'}, {'answer_id': 12466, 'author': 'jcsalterego', 'author_id': 1416, 'author_profile': 'https://Stackoverflow.com/users/1416', 'pm_score': 4, 'selected': True, 'text': "<p>Overview:</p>\n\n<p>A <strong>naïve!!</strong> algorithm keeps track of the frequency of words in a per-column manner, where one can assume that each line can be separated into columns with a delimiter.</p>\n\n<p>Example input:</p>\n\n<blockquote>\n  <p>The dog jumped over the moon<br>\n  The cat jumped over the moon<br>\n  The moon jumped over the moon<br>\n  The car jumped over the moon  </p>\n</blockquote>\n\n<p>Frequencies:</p>\n\n<pre><code>Column 1: {The: 4}\nColumn 2: {car: 1, cat: 1, dog: 1, moon: 1}\nColumn 3: {jumped: 4}\nColumn 4: {over: 4}\nColumn 5: {the: 4}\nColumn 6: {moon: 4}\n</code></pre>\n\n<p>We could partition these frequency lists further by grouping based on the total number of fields, but in this simple and convenient example, we are only working with a fixed number of fields (6).</p>\n\n<p>The next step is to iterate through lines which generated these frequency lists, so let's take the first example.</p>\n\n<ol>\n<li><strong>The</strong>: meets some hand-wavy criteria and the algorithm decides it must be static.</li>\n<li><strong>dog</strong>: doesn't appear to be static based on the rest of the frequency list, and thus it must be dynamic as opposed to static text.  We loop through a few pre-defined regular expressions and come up with <code>/[a-z]+/i</code>.</li>\n<li><strong>over</strong>: same deal as #1; it's static, so leave as is.</li>\n<li><strong>the</strong>: same deal as #1; it's static, so leave as is.</li>\n<li><strong>moon</strong>: same deal as #1; it's static, so leave as is.</li>\n</ol>\n\n<p>Thus, just from going over the first line we can put together the following regular expression:</p>\n\n<pre><code>/The ([a-z]+?) jumps over the moon/\n</code></pre>\n\n<p>Considerations:</p>\n\n<ul>\n<li><p>Obviously one can choose to scan part or the whole document for the first pass, as long as one is confident the frequency lists will be a sufficient sampling of the entire data.</p></li>\n<li><p>False positives may creep into the results, and it will be up to the filtering algorithm (hand-waving) to provide the best threshold between static and dynamic fields, or some human post-processing.</p></li>\n<li><p>The overall idea is probably a good one, but the actual implementation will definitely weigh in on the speed and efficiency of this algorithm.</p></li>\n</ul>\n"}, {'answer_id': 12558, 'author': 'Anders Eurenius', 'author_id': 1421, 'author_profile': 'https://Stackoverflow.com/users/1421', 'pm_score': 0, 'selected': False, 'text': '<p>@Derek Park: Well, even a strong AI couldn\'t be sure it had the right answer.</p>\n\n<p>Perhaps some compression-like mechanism could be used:</p>\n\n<ol start="2">\n<li>Find large, frequent substrings</li>\n<li>Find large, frequent substring patterns. (i.e. [pattern:1] [junk] [pattern:2])</li>\n</ol>\n\n<p>Another item to consider might be to group lines by <a href="http://en.wikipedia.org/wiki/Edit_distance" rel="nofollow noreferrer">edit-distance</a>. Grouping similar lines should split the problem into one-pattern-per-group chunks.</p>\n\n<p>Actually, if you manage to write this, <em>let the whole world know</em>, I think a lot of us would like this tool!</p>\n'}, {'answer_id': 12566, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 0, 'selected': False, 'text': '<p>@Anders</p>\n\n<blockquote>\n  <p>Well, even a strong AI couldn\'t be sure it had the right answer.</p>\n</blockquote>\n\n<p>I was thinking that sufficiently strong AI could <em>usually</em> figure out the right answer from the context.  e.g. Strong AI could recognize that "35F" in this context is a temperature and not a hex number.  There are definitely cases where even strong AI would be unable to answer.  Those are the same cases where a human would be unable to answer, though (assuming <em>very</em> strong AI).</p>\n\n<p>Of course, it doesn\'t really matter, since we don\'t have strong AI.  :)</p>\n'}, {'answer_id': 119150, 'author': 'pngaz', 'author_id': 10972, 'author_profile': 'https://Stackoverflow.com/users/10972', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www.logparser.com" rel="nofollow noreferrer">http://www.logparser.com</a> forwards to an IIS forum which seems fairly active. This is the official site for Gabriele Giuseppini\'s "Log Parser Toolkit".   While I have never actually used this tool, I did pick up a cheap copy of the book from Amazon Marketplace - today a copy is as low as $16.  Nothing beats a dead-tree-interface for just flipping through pages.<p>\nGlancing at this forum, I had not previously heard about the "New GUI tool for MS Log Parser, Log Parser Lizard" at <a href="http://www.lizardl.com/" rel="nofollow noreferrer">http://www.lizardl.com/</a>.<p>\nThe key issue of course is the complexity of your GRAMMAR.  To use any kind of log-parser as the term is commonly used, you need to know exactly what you\'re scanning for, you can write a BNF for it.  Many years ago I took a course based on Aho-and-Ullman\'s "Dragon Book", and the thoroughly understood LALR technology can give you optimal speed, provided of course that you have that CFG.<p>\nOn the other hand it does seem you\'re possibly reaching for something AI-like, which is a different order of complexity entirely.</p>\n'}]
['https://Stackoverflow.com/questions/9882', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']