<?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: Fix replace command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476174#M133744</link>
    <description>&lt;P&gt;I am not sure what is wrong but just do it another way, like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=metrics_total mode=sed "s/Total:\s*//"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 Nov 2019 03:54:45 GMT</pubDate>
    <dc:creator>woodcock</dc:creator>
    <dc:date>2019-11-07T03:54:45Z</dc:date>
    <item>
      <title>Fix replace command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476171#M133741</link>
      <description>&lt;H3&gt;Background&lt;/H3&gt;

&lt;P&gt;I have a log file where I have extracted some fields. I am trying to parse a field to get the numeric values it has using replace but it is not working and I don't understand why.&lt;/P&gt;

&lt;H3&gt;Problem&lt;/H3&gt;

&lt;P&gt;I have a long log file and one of the fields I extracted is called &lt;CODE&gt;metrics_total&lt;/CODE&gt; and has the following format: &lt;CODE&gt;"Total: __decimal_number__"&lt;/CODE&gt;, where decimal number is any floating point number. &lt;/P&gt;

&lt;P&gt;My objective is to create an average of this field, but because I have the string &lt;CODE&gt;"Total: "&lt;/CODE&gt; the &lt;CODE&gt;avg&lt;/CODE&gt; command fails. So I am trying to remove it using &lt;CODE&gt;replace&lt;/CODE&gt;. However I am failing.&lt;/P&gt;

&lt;H3&gt;Query&lt;/H3&gt;

&lt;P&gt;This is how I am trying to use replace:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;&lt;BR /&gt;
host=host00 OR host01 endpoint=* http_method=* http_status=200 metrics_total=* | replace "Total: " with "" in metrics_total | table http_method endpoint metrics_total&lt;BR /&gt;
&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Where &lt;CODE&gt;host&lt;/CODE&gt;, &lt;CODE&gt;endpoint&lt;/CODE&gt;, &lt;CODE&gt;http_method&lt;/CODE&gt;, &lt;CODE&gt;http_status&lt;/CODE&gt; and &lt;CODE&gt;metrics_total&lt;/CODE&gt; are extracted fields.&lt;/P&gt;

&lt;P&gt;The issue here is that no matter what I do, nothing changes. This is what I get: &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;&lt;BR /&gt;
GET /product/bananas    Total: 0.087&lt;BR /&gt;
GET /product/apples         Total: 0.003&lt;BR /&gt;
GET /cart/checkout          Total: 0.005&lt;BR /&gt;
&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;And this is what I &lt;EM&gt;actually&lt;/EM&gt; want to achieve:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;&lt;BR /&gt;
GET /product/bananas    0.087&lt;BR /&gt;
GET /product/apples         0.003&lt;BR /&gt;
GET /cart/checkout          0.005&lt;BR /&gt;
&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Here I would get only the numbers instead of the whole &lt;CODE&gt;Total: 0.087&lt;/CODE&gt; string.&lt;/P&gt;

&lt;H3&gt;Going further&lt;/H3&gt;

&lt;P&gt;Going even further I would really like to have this field computed into an average. As in, the &lt;CODE&gt;avg(metrics_total)&lt;/CODE&gt;for each endpoint grouped by &lt;CODE&gt;http_method&lt;/CODE&gt;.&lt;/P&gt;

&lt;H3&gt;Questions&lt;/H3&gt;

&lt;OL&gt;
&lt;LI&gt;What is wrong in my usage of replace?&lt;/LI&gt;
&lt;LI&gt;How can I compute the average metric for each endpoint grouped by http_method?&lt;/LI&gt;
&lt;LI&gt;Is there an easier way to achieve my objective? (Am I complicating things too much?)&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 06 Nov 2019 13:00:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476171#M133741</guid>
      <dc:creator>pedroma</dc:creator>
      <dc:date>2019-11-06T13:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: Fix replace command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476172#M133742</link>
      <description>&lt;P&gt;@pedroma,&lt;/P&gt;

&lt;P&gt;Try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"Your search"
|rex field=metrics_total "(?&amp;lt;Total&amp;gt;\d+.\d+)"
|stats avg(Total) as avg_by_method by http_method
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;CODE&gt;replace&lt;/CODE&gt; works well with full string replacements&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 13:17:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476172#M133742</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2019-11-06T13:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Fix replace command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476173#M133743</link>
      <description>&lt;P&gt;Thanks for the query! Could you explain me why my &lt;CODE&gt;replace&lt;/CODE&gt; was incorrect and why using &lt;CODE&gt;rex&lt;/CODE&gt; was better for my use case?&lt;/P&gt;

&lt;P&gt;I don't quite understand what you mean with "full string replacements".&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 15:15:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476173#M133743</guid>
      <dc:creator>pedroma</dc:creator>
      <dc:date>2019-11-06T15:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: Fix replace command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476174#M133744</link>
      <description>&lt;P&gt;I am not sure what is wrong but just do it another way, like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=metrics_total mode=sed "s/Total:\s*//"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Nov 2019 03:54:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Fix-replace-command/m-p/476174#M133744</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-11-07T03:54:45Z</dc:date>
    </item>
  </channel>
</rss>

