<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: regular expression in my lookup table in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107015#M27832</link>
    <description>&lt;P&gt;I want the Name UserDefinedCategory should be displayed in the category...but this is not workin ?? am i missin something ??&lt;/P&gt;</description>
    <pubDate>Mon, 04 Nov 2013 14:37:58 GMT</pubDate>
    <dc:creator>rakesh_498115</dc:creator>
    <dc:date>2013-11-04T14:37:58Z</dc:date>
    <item>
      <title>regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107009#M27826</link>
      <description>&lt;P&gt;hi,all,here is my problem:&lt;/P&gt;

&lt;P&gt;here is my search:&lt;BR /&gt;&lt;BR /&gt;
mysearch | table fields1 fields2&lt;BR /&gt;&lt;BR /&gt;
and I got:  &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;&lt;STRONG&gt;fields1     fields2&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;
foofoo      abcccd&lt;BR /&gt;&lt;BR /&gt;
barbar      asdddf  &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;the lookup table I define in lookups is as below,the keywords is &lt;STRONG&gt;&lt;EM&gt;regular expression&lt;/EM&gt;&lt;/STRONG&gt; which I want match the fields2&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;&lt;STRONG&gt;keyword   fields3&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;
abccc\w+      10&lt;BR /&gt;&lt;BR /&gt;
asddd\w+    20  &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;what I want is&lt;BR /&gt;&lt;BR /&gt;
&lt;STRONG&gt;fields1  fields2  fields3&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;
foofoo   abcccd    10&lt;BR /&gt;&lt;BR /&gt;
barbar   asdddf    20&lt;/P&gt;

&lt;P&gt;so how can I get this done?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2013 07:26:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107009#M27826</guid>
      <dc:creator>fengl2</dc:creator>
      <dc:date>2013-04-26T07:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107010#M27827</link>
      <description>&lt;P&gt;There is no regex support in static lookup tables unfortunately. You could achieve this by writing a dynamic lookup script that does this, the obvious drawback obviously being that it's a bit more hassle to roll up your sleeves and start coding.&lt;/P&gt;

&lt;P&gt;I've written this kind of dynamic lookup for this exact purpose and have it lying around somewhere, but don't know where right now - let me know if you want it and I'll have another look.&lt;/P&gt;

&lt;P&gt;EDIT: So, looked around and found it. DISCLAIMER, I'm by no means a real Python coder &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/python

# A dynamic lookup that takes CSV as input, performs a regex match against another CSV, then returns the CSV results                                                          
import csv
import sys
import re
import os
import glob

def inlookup(inf, inval, outf):
    try:
        # The app makes the assumption that a directory in the form &amp;lt;customer&amp;gt;_indexer_config exists. If multiple
        # directories matching this template exist for some weird reason, only the first one is used.
        config_app_path = os.path.join(os.environ['SPLUNK_HOME'],'etc','apps','yourapp')
        csvname = "yourlookup.csv"
        csvpath = os.path.join(config_app_path,'lookups',csvname)
    except Exception as e:
        sys.stderr.write("No %s file found." % csvname)
        sys.exit(0)

    try:
        c = open(csvpath, 'rb')
        f = csv.DictReader(c)

        for row in f:
            if re.search(row[inf], inval):
                return row[outf]

    except Exception as e:
        sys.stderr.write(e)
        sys.exit(1)
        return []


def main():
    if len(sys.argv) != 3:
        print "Usage: %s &amp;lt;in field&amp;gt; &amp;lt;out field&amp;gt;" % (sys.argv[0])
        sys.exit(0)

    inf = sys.argv[1]
    outf = sys.argv[2]
    r = csv.DictReader(sys.stdin)
    w = csv.DictWriter(sys.stdout, r.fieldnames)
    w.writeheader()

    for result in r:
        # If all fields are already present, there's no need
        # to look anything up
        if len(result[inf]) and len(result[outf]):
            w.writerow(result)

        elif len(result[inf]):
            outvalue = inlookup(inf, result[inf], outf)
            result[outf] = outvalue
            w.writerow(result)


main()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;As you can see in the start of the &lt;CODE&gt;inlookup&lt;/CODE&gt; function you need to specify your path and lookup filename explicitly. As far as I know there's unfortunately no way of providing an argument for a lookup to consume it that way, so it needs to be hardcoded.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2013 07:58:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107010#M27827</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2013-04-26T07:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107011#M27828</link>
      <description>&lt;P&gt;yeah,I really appreciate it if you could have another look,the problem I mentioned is a real case in my work and I stuck here.By the way I write some python script in my daily work,thanks in advance if you could provide the answer!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2013 13:43:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107011#M27828</guid>
      <dc:creator>fengl2</dc:creator>
      <dc:date>2013-04-26T13:43:23Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107012#M27829</link>
      <description>&lt;P&gt;Amended my answer with the code I found lying around... &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2013 14:15:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107012#M27829</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2013-04-26T14:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107013#M27830</link>
      <description>&lt;P&gt;thanks,it is very helpful!&lt;/P&gt;</description>
      <pubDate>Sat, 27 Apr 2013 02:34:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107013#M27830</guid>
      <dc:creator>fengl2</dc:creator>
      <dc:date>2013-04-27T02:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107014#M27831</link>
      <description>&lt;P&gt;Hi Ayn,&lt;/P&gt;

&lt;P&gt;Can you pls give me the steps in exucting this ?&lt;/P&gt;

&lt;P&gt;I have done the following , but this seems not working &lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;I have copied above in my app's bin directory i.e /opt/splunk/etc/apps/MY_APP/bin/regexpython.py with the filename regexpython.py&lt;/LI&gt;
&lt;LI&gt;I have my lookup file name lookup_UniqueId.csv , which has fields Id, Name&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Id is the value that comes in the logs, and correspondingly it matches the Name that are present in the lookup file&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;Now with ur code of regex . i have added this line in my lookup
Id,Name
^2\d+6$,"UserDefinedCategory"&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;ie. if my Id is starting with 2 and ends with 6&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2013 14:37:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107014#M27831</guid>
      <dc:creator>rakesh_498115</dc:creator>
      <dc:date>2013-11-04T14:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107015#M27832</link>
      <description>&lt;P&gt;I want the Name UserDefinedCategory should be displayed in the category...but this is not workin ?? am i missin something ??&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2013 14:37:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107015#M27832</guid>
      <dc:creator>rakesh_498115</dc:creator>
      <dc:date>2013-11-04T14:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107016#M27833</link>
      <description>&lt;P&gt;transforms.conf&lt;/P&gt;

&lt;P&gt;[UniqueID_Lookup]&lt;BR /&gt;
external_cmd = regexpython.py Id,Name&lt;BR /&gt;
external_type = python&lt;BR /&gt;
fields_list = Id,Name&lt;/P&gt;

&lt;P&gt;props.conf&lt;/P&gt;

&lt;P&gt;LOOKUP-UniqueID_Lookup = UniqueID_Lookup Id AS Id OUTPUTNEW Name AS Name&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:12:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107016#M27833</guid>
      <dc:creator>rakesh_498115</dc:creator>
      <dc:date>2020-09-28T15:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107017#M27834</link>
      <description>&lt;P&gt;Hi all we have some trouble with this python script&lt;BR /&gt;
Splunk error code&lt;BR /&gt;
"returned error code 1"&lt;/P&gt;

&lt;P&gt;Please Help&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jan 2015 09:18:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107017#M27834</guid>
      <dc:creator>tony_alibelli</dc:creator>
      <dc:date>2015-01-13T09:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: regular expression in my lookup table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107018#M27835</link>
      <description>&lt;P&gt;I just thought it may be worth pointing out that the &lt;CODE&gt;mvrex&lt;/CODE&gt; command which is implemented by the &lt;A href="https://splunkbase.splunk.com/app/2968/"&gt;SA-cim_validator&lt;/A&gt; app may be something worth taking a look at.  While the command itself doesn't deal with lookups, values pulled back from lookups are send through this command on at least one of the dashboards:&lt;/P&gt;

&lt;P&gt;&lt;A href="https://github.com/hire-vladimir/SA-cim_validator/blob/master/bin/mvrex.py"&gt;https://github.com/hire-vladimir/SA-cim_validator/blob/master/bin/mvrex.py&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Anyways, the combo of regex within lookups is pretty rare.  Thought this may give some future readers some ideas to think about.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2017 17:47:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/regular-expression-in-my-lookup-table/m-p/107018#M27835</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2017-02-10T17:47:25Z</dc:date>
    </item>
  </channel>
</rss>

