<?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 How to define a calculated field based on chained rex statements in Splunk Web? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207607#M60518</link>
    <description>&lt;P&gt;I'm using Splunk Enterprise.  I have a search that looks like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=foo sourcetype=yapache_access host=bar  | fields url,duration  | rex field=url mode=sed "s/[a-zA-Z0-9._]{20,}/_HASH/g" | rex field=url mode=sed "s/ysp_user_agent=[^&amp;amp;]+//g"   | rex field=url mode=sed "s/oauth[a-z_]+=[a-zA-Z0-9_]+//g"   | rex field=url mode=sed "s/(\d\d\d\d-\d\d-\d\d)/YYYY-MM-DD/g"   | rex field=url mode=sed "s/([.\/=;,])(\d+)/\1_ID/g" | stats count, avg(duration) as servertime by url | where count&amp;gt;100 | sort 100 -servertime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This search groups urls by replacing embedded id's and dates, etc with constants so that I can look at requests that have at least 100 uses, and then sort them by their mean servertime to find slow requests.&lt;/P&gt;

&lt;P&gt;I would like to share out this flattening of the url to other users on the team in a convenient to use way.    So, two questions:&lt;/P&gt;

&lt;P&gt;1) is defining a new calculated field via the UI: "&lt;EM&gt;Fields » Calculated fields » Add new&lt;/EM&gt;" the way to go?&lt;BR /&gt;
2) if so, how to do I do it?  I haven't found an example that shows me how to fill out that form when a chain of rex's is what defines my new field.&lt;/P&gt;

&lt;P&gt;Apologies if this is detailed somewhere handy.  I tried searching the docs and the forums before asking this.&lt;/P&gt;</description>
    <pubDate>Thu, 18 Feb 2016 03:34:53 GMT</pubDate>
    <dc:creator>floppymoose</dc:creator>
    <dc:date>2016-02-18T03:34:53Z</dc:date>
    <item>
      <title>How to define a calculated field based on chained rex statements in Splunk Web?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207607#M60518</link>
      <description>&lt;P&gt;I'm using Splunk Enterprise.  I have a search that looks like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=foo sourcetype=yapache_access host=bar  | fields url,duration  | rex field=url mode=sed "s/[a-zA-Z0-9._]{20,}/_HASH/g" | rex field=url mode=sed "s/ysp_user_agent=[^&amp;amp;]+//g"   | rex field=url mode=sed "s/oauth[a-z_]+=[a-zA-Z0-9_]+//g"   | rex field=url mode=sed "s/(\d\d\d\d-\d\d-\d\d)/YYYY-MM-DD/g"   | rex field=url mode=sed "s/([.\/=;,])(\d+)/\1_ID/g" | stats count, avg(duration) as servertime by url | where count&amp;gt;100 | sort 100 -servertime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This search groups urls by replacing embedded id's and dates, etc with constants so that I can look at requests that have at least 100 uses, and then sort them by their mean servertime to find slow requests.&lt;/P&gt;

&lt;P&gt;I would like to share out this flattening of the url to other users on the team in a convenient to use way.    So, two questions:&lt;/P&gt;

&lt;P&gt;1) is defining a new calculated field via the UI: "&lt;EM&gt;Fields » Calculated fields » Add new&lt;/EM&gt;" the way to go?&lt;BR /&gt;
2) if so, how to do I do it?  I haven't found an example that shows me how to fill out that form when a chain of rex's is what defines my new field.&lt;/P&gt;

&lt;P&gt;Apologies if this is detailed somewhere handy.  I tried searching the docs and the forums before asking this.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Feb 2016 03:34:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207607#M60518</guid>
      <dc:creator>floppymoose</dc:creator>
      <dc:date>2016-02-18T03:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to define a calculated field based on chained rex statements in Splunk Web?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207608#M60519</link>
      <description>&lt;P&gt;There's a great document by the docs team to &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.1/Knowledge/Createandmaintainsearch-timefieldextractionsthroughconfigurationfiles"&gt;Create and maintain search-time field extractions through configuration files&lt;/A&gt;.  But I don't think this is what you need because you are "erasing" parts of a line, and unless you want to erase the actual stuff in the event sort-of-permanently, this might be difficult.&lt;/P&gt;

&lt;P&gt;Still, here's what I'd do: &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.3/Knowledge/Defineandusesearchmacros"&gt;create a macro&lt;/A&gt;!&lt;/P&gt;

&lt;P&gt;You can probably take that entire pile of &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; rex ... | rex ... | rex ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And create a macro from that.  If you name it 'CleanUpURL' then you can call it in your actual search (or someone else can) like so:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=foo sourcetype=yapache_access host=bar  | fields url,duration  | `CleanUpURL` | stats count, avg(duration) as servertime by url | where count&amp;gt;100 | sort 100 -servertime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;One tip: watch your leading and trailing pipes &lt;CODE&gt;|&lt;/CODE&gt; - you can include them in the macro or not, but stay consistent.  You obviously have to keep them in the MIDDLE of your macro, it's just the ones at the ends.  I usually do it the way I describe, but you could also do it this way:&lt;/P&gt;

&lt;P&gt;Macro:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex ... | rex ... | rex ... |
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;New search using macro:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=foo sourcetype=yapache_access host=bar  | fields url,duration  `CleanUpURL` stats count, avg(duration) as servertime by url | where count&amp;gt;100 | sort 100 -servertime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I personally think the FIRST way is way cleaner and easier to follow.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2016 01:45:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207608#M60519</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2016-02-19T01:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to define a calculated field based on chained rex statements in Splunk Web?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207609#M60520</link>
      <description>&lt;P&gt;Thanks, this steered me in a useful direction!&lt;BR /&gt;
I tried to give you karma points but I get an alert saying the maximum I can award is 0.  &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2016 23:08:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207609#M60520</guid>
      <dc:creator>floppymoose</dc:creator>
      <dc:date>2016-02-19T23:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to define a calculated field based on chained rex statements in Splunk Web?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207610#M60521</link>
      <description>&lt;P&gt;If @rich7177's answer has resolved your issue, you can accept his answer (by clicking on little tick mark Accept link below the answer and reward him by voting up the answer. &lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2016 23:25:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207610#M60521</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-02-19T23:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to define a calculated field based on chained rex statements in Splunk Web?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207611#M60522</link>
      <description>&lt;P&gt;Do share the direction you went, so others can benefit too.&lt;/P&gt;

&lt;P&gt;Side note: You should be able to substitute &lt;CODE&gt;replace(field, regex, "replacement")&lt;/CODE&gt; for your &lt;CODE&gt;rex mode=sed&lt;/CODE&gt; calls, those should enable your original thought of adding a calculated field. I'd personally prefer that over having to teach people how to use the macro, and people having to use the macro everywhere.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2016 17:02:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-define-a-calculated-field-based-on-chained-rex-statements/m-p/207611#M60522</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2016-02-26T17:02:36Z</dc:date>
    </item>
  </channel>
</rss>

