Python - Extract strings from a log file and write them into another file
<p>I've got a log file like below:</p>

<pre><code>sw2 switch_has sw2_p3.
sw1 transmits sw2_p2
/* BUG: axiom too complex: SubClassOf(ObjectOneOf([NamedIndividual(#t_air_sens2)]),DataHasValue(DataProperty(#qos_type),^^(latency,http://www.xcx.org/1900/02/22-rdf-syntax-ns#PlainLiteral))) */
/* BUG: axiom too complex: SubClassOf(ObjectOneOf([NamedIndividual(#t_air_sens2)]),DataHasValue(DataProperty(#topic_type),^^(periodic,http://www.xcx.org/1901/11/22-rdf-syntax-ns#PlainLiteral))) */
...
</code></pre>

<p>what I'm interested in, is to extract specific words from <code>/* BUG...</code> lines and write them into separate file, something like below:</p>

<pre><code>t_air_sens2 qos_type latency
t_air_sens2 topic_type periodic
...
</code></pre>

<p>I can do this with the help of <code>awk</code> and regex in shell like below:</p>

<pre><code>awk -F'#|\\^\\^\\(' '{for (i=2; i&lt;NF; i++) printf "%s%s", gensub(/[^[:alnum:]_].*/,"",1,$i), (i&lt;(NF-1) ? OFS : ORS) }' output.txt &gt; ./LogErrors/Properties.txt
</code></pre>

<p>How can I extract them using Python? (shall I use regex again, or..?)</p>

<p>You can of course use regex. I would read line by line, grab the lines the start with <code>'/* BUG:'</code>, then parse those as needed.</p>

<pre><code>import re

target = r'/* BUG:'
bugs = []
with open('logfile.txt', 'r') as infile, open('output.txt', 'w') as outfile:
    # loop through logfile
    for line in infile:
        if line.startswith(target):
            # add line to bug list and strip newlines
            bugs.append(line.strip())
            # or just do regex parsing here
            # create match pattern groups with parentheses, escape literal parentheses with '\'
            match = re.search(r'NamedIndividual\(([\w#]+)\)]\),DataHasValue\(DataProperty\(([\w#]+)\),\^\^\(([\w#]+),', line)
            # if matches are found
            if match:
                # loop through match groups, write to output
                for group in match.groups():
                    outfile.write('{} '.format(group))
                outfile.write('\n')
</code></pre>

<p>Python has a pretty powerful regex module built-in: <a href="https://docs.python.org/3/library/re.html" rel="nofollow">re module</a></p>

<p>You can <a href="https://docs.python.org/3/library/re.html#match-objects" rel="nofollow">search for a given pattern, then print out the matched groups as needed</a>.</p>

<p>Note: <a href="https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals" rel="nofollow">raw strings</a> (<code>r'xxxx'</code>) let you use unescaped characters.</p>

python file