Extract multiple substrings from a file and list them in another place using python/shell
<p>I've got a log file similar to below:</p>

<pre><code>/* BUG: axiom too complex: SubClassOf(ObjectOneOf([NamedIndividual(http://www.sem.org/sina/onto/2015/7/TSB-GCL#t_Xi_xi)]),DataHasValue(DataProperty(http://www.code.org/onto/ont.owl#XoX_type),^^(periodic,http://www.mdos.org/1956/21/2-rdf-syntax-ns#PlainLiteral))) */
/* BUG: axiom too complex: SubClassOf(ObjectOneOf([NamedIndividual(http://www.sem.org/sina/onto/2015/7/TSB-GCL#t_Ziz)]),DataHasValue(DataProperty(http://www.co-ode.org/ontologies/ont.owl#YoY_type),^^(latency,http://www.w3.org/1956/01/11-rdf-syntax-ns#PlainLiteral))) */
....
</code></pre>

<p>I want to extract the fields of <em>t_Xi_xi</em>, <em>t_Ziz</em> ,<em>XoX_type</em> and <em>YoY_type</em> and also the values after <em>^^(</em> which in this case are <em>latency</em> and <em>periodic</em>.</p>

<p>Note: There are different alphabetic values for each <em>X</em> and <em>Y</em> in the file (e.g. X="sina" Y="Boom" so --> t_Xi_xi ~ t_Sina_sina) so I guess using the regex would be a better choice.</p>

<p>So the final result must be something like below:</p>

<pre><code>t_Xi_xi    XoX_type    periodic
t_Ziz    YoY_type    latency
</code></pre>

<p>I've tried the regex below to extract them and hopefully to be able to replace the rest of it to " " in the file with the help of <em>sed</em> in shell, but I failed.</p>

<pre><code>([a-zA-Z]_[a-zA-Z]*_[a-zA-Z]*)|(\#[a-zA-Z]*_[a-zA-Z]*)|(\^\([a-zA-Z]*)+
</code></pre>

<p>Any kind of help is appreciated on how to do this in Python (or even shell itself).</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) }' file
t_Xi_xi XoX_type periodic
t_Ziz YoY_type latency
</code></pre>

<p>The above uses GNU awk for gensub(), with other awks you'd use sub() and a separate printf statement.</p>

python string shell sed substring