<?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: How to write a Calculated field by extracting a value from an existing field using REGEX in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263828#M79184</link>
    <description>&lt;P&gt;But you are right Vinod, rex is a bit problematic in this case... &lt;/P&gt;</description>
    <pubDate>Tue, 29 Mar 2016 19:36:31 GMT</pubDate>
    <dc:creator>ddrillic</dc:creator>
    <dc:date>2016-03-29T19:36:31Z</dc:date>
    <item>
      <title>How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263825#M79181</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;

&lt;P&gt;I'd want to create a calculated field called &lt;STRONG&gt;domainName&lt;/STRONG&gt; from the &lt;STRONG&gt;SourcePath&lt;/STRONG&gt; field.&lt;BR /&gt;
SourcePath field has this value: "/org/app/comp_&lt;STRONG&gt;domain123&lt;/STRONG&gt;_port_host"&lt;/P&gt;

&lt;P&gt;I need to extract the "domain123" string from the above field and store it in domainName calculated field&lt;BR /&gt;
I had to probably write an eval expression since I had to store this field under "calculated fields" settings in Splunk.&lt;BR /&gt;
But with eval, we cannot use rex I suppose, so how do I achieve this? Read some examples that we can use mvfilter along with a match function, but it didn't seem to work. &lt;/P&gt;

&lt;P&gt;Any help would be appreciated &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Regards,&lt;BR /&gt;
Vinod&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 09:13:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263825#M79181</guid>
      <dc:creator>apvinod2003</dc:creator>
      <dc:date>2020-09-29T09:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263826#M79182</link>
      <description>&lt;P&gt;You can use rex:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| yoursearch
| rex field=SourcePath "(?msi)^\/[^\/]+\/[^\/]+\/[^_]+_(?&amp;lt;domainName&amp;gt;[^_]+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;For example, the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| eval SourcePath = "/org/app/comp_domain123_port_host"
| rex field=SourcePath "(?msi)^\/[^\/]+\/[^\/]+\/[^_]+_(?&amp;lt;domainName&amp;gt;[^_]+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Will create a new field called domainName with value domain123&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 19:23:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263826#M79182</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-03-29T19:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263827#M79183</link>
      <description>&lt;P&gt;Vinod, you can do it in the calculated fields settings by specifying the new field name in Name and the eval expression in the Eval expression field. I just tried it by placing replace(adjd_dt,"-","") there and it worked just fine.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 19:28:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263827#M79183</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2016-03-29T19:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263828#M79184</link>
      <description>&lt;P&gt;But you are right Vinod, rex is a bit problematic in this case... &lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 19:36:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263828#M79184</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2016-03-29T19:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263829#M79185</link>
      <description>&lt;P&gt;To my knowedge you cannot do this with an eval. I know this is not as convenient as a calculated field, but have you considered writing a rex command like the below and save it as a macro? That way you can invoke the extraction very quickly when needed. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;mysearch | rex field=SourcePath "\/\w*\/\w*\/[a-zA-Z]*\_(?&amp;lt;domainName&amp;gt;[a-zA-Z0-9]*)"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2016 19:41:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263829#M79185</guid>
      <dc:creator>jedatt01</dc:creator>
      <dc:date>2016-03-29T19:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263830#M79186</link>
      <description>&lt;P&gt;Any particular reason you need to this with a calculated field versus a field extraction? If you're open to the field extraction, then jedatt01's solution can be leveraged under Settings -&amp;gt; Field Extractions. You can set it as inline, which will end up being an EXTRACT in props.conf.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 19:44:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263830#M79186</guid>
      <dc:creator>jsie_splunk</dc:creator>
      <dc:date>2016-03-29T19:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263831#M79187</link>
      <description>&lt;P&gt;Combining rex from @javiergn and replace function from @ddrillic, use this as your calculated field definition (if updating using props.conf) OR just copy the part after EVAL-&lt;BR /&gt;
props.conf &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[yoursourcetype]
EVAL-domain=replace(SourcePath,"^\/[^\/]+\/[^\/]+\/[^_]+_([^_]+)","\1")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2016 22:49:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263831#M79187</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-03-29T22:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a Calculated field by extracting a value from an existing field using REGEX</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263832#M79188</link>
      <description>&lt;P&gt;Thanks all for your answers; didn't expect so many answers in a short span &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
I now realize that Field Extraction would be a better choice, given that I actually need to be able to search on that extracted field (Calculated field is more for projecting the result I guess, I may be wrong).&lt;BR /&gt;
I had created an extracted field called "domainName" through "settings -&amp;gt; Fields -&amp;gt; Extracted Fields" as below:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex field=SourcePath "\/\w*\/\w*\/[a-zA-Z]*\_(?&amp;lt;domainName&amp;gt;[a-zA-Z0-9]*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I had enabled read permissions for "search" app. But I'm not able to search on this domainName&lt;BR /&gt;
when I search with &lt;EM&gt;host=host123 domainName=domain123&lt;/EM&gt; , I don't get any results.&lt;/P&gt;

&lt;P&gt;Search with &lt;CODE&gt;host=host123 SourcePath = "/org/app/comp_domain123_port_host"&lt;/CODE&gt; returns results though. &lt;/P&gt;

&lt;P&gt;Any clues ?&lt;/P&gt;

&lt;P&gt;Another query: I don't find the "portName" extracted field in Selected fields, or interested fields or all fields sections. Is any configuration required to make them appear there?&lt;/P&gt;</description>
      <pubDate>Wed, 30 Mar 2016 14:29:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-Calculated-field-by-extracting-a-value-from-an/m-p/263832#M79188</guid>
      <dc:creator>apvinod2003</dc:creator>
      <dc:date>2016-03-30T14:29:41Z</dc:date>
    </item>
  </channel>
</rss>

