<?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 create new field based on regex? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684583#M233679</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Thanks &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/206061"&gt;@scelikok&lt;/a&gt;. No I don't just want the orderID. But I want to manually create the RESTful API routing pattern.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for "path=/order/123456",&amp;nbsp; "route=/order/{orderID}", basically I am trying to use regex to replace the value and create a new field in this way:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if value matches&amp;nbsp;\/order\/\d{12}, then convert to /order/{orderID}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have other examples like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;path=/user/jason@sample.com/orders&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;route=/user/{userID}/orders&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 18 Apr 2024 21:13:46 GMT</pubDate>
    <dc:creator>codewarrior</dc:creator>
    <dc:date>2024-04-18T21:13:46Z</dc:date>
    <item>
      <title>How to create new field based on regex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684562#M233670</link>
      <description>&lt;P&gt;I have a log stream in this format:&lt;/P&gt;&lt;PRE&gt;level=info request.elapsed=100 request.method=GET request.path=/orders/123456 request_id=2ca011b5-ad34-4f32-a95c-78e8b5b1a270 response.status=500&lt;/PRE&gt;&lt;P&gt;I have extracted the fields using regex:&lt;/P&gt;&lt;PRE&gt;| rex field=message "level=info request.elapsed=(?&amp;lt;duration&amp;gt;.*) request.method=(?&amp;lt;method&amp;gt;.*) request.path=(?&amp;lt;path&amp;gt;.*) request_id=(?&amp;lt;request_id&amp;gt;.*) response.status=(?&amp;lt;statusCode&amp;gt;.*)"&lt;/PRE&gt;&lt;P&gt;I want to manually build a new field called route based on the extracted field path. For example, for "path=/order/123456", I want to create new field "route=/order/{orderID}", so I can grouping by route not by path, the path contains real parameter which I cannot group on path.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I achieve this? Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 21:03:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684562#M233670</guid>
      <dc:creator>codewarrior</dc:creator>
      <dc:date>2024-04-17T21:03:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new field based on regex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684577#M233675</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/235510"&gt;@codewarrior&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;If I got it correct, your need is extract a new field named "route" and it will contain the value after "orders/".&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can capture it in your rex command, please try below;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;level=info request.elapsed=(?&amp;lt;duration&amp;gt;.*) request.method=(?&amp;lt;method&amp;gt;.*) request.path=(?&amp;lt;path&amp;gt;.+orders\/(?&amp;lt;route&amp;gt;.+)) request_id=(?&amp;lt;request_id&amp;gt;.*) response.status=(?&amp;lt;statusCode&amp;gt;.*)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 22:32:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684577#M233675</guid>
      <dc:creator>scelikok</dc:creator>
      <dc:date>2024-04-17T22:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new field based on regex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684583#M233679</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thanks &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/206061"&gt;@scelikok&lt;/a&gt;. No I don't just want the orderID. But I want to manually create the RESTful API routing pattern.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for "path=/order/123456",&amp;nbsp; "route=/order/{orderID}", basically I am trying to use regex to replace the value and create a new field in this way:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if value matches&amp;nbsp;\/order\/\d{12}, then convert to /order/{orderID}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have other examples like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;path=/user/jason@sample.com/orders&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;route=/user/{userID}/orders&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 21:13:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684583#M233679</guid>
      <dc:creator>codewarrior</dc:creator>
      <dc:date>2024-04-18T21:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new field based on regex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684589#M233682</link>
      <description>&lt;P&gt;I do not see why you needed to do that extra extraction because Splunk should have given you a field named "request_path" already. (See emulation below.) &amp;nbsp;All you need to do is to assign a new field based on match.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval route = if(match(request_path, "^/orders/\d+"), "/order/{orderID}", null())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The sample data should give you something like&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;level&lt;/TD&gt;&lt;TD&gt;request_elapsed&lt;/TD&gt;&lt;TD&gt;request_id&lt;/TD&gt;&lt;TD&gt;request_method&lt;/TD&gt;&lt;TD&gt;request_path&lt;/TD&gt;&lt;TD&gt;response_status&lt;/TD&gt;&lt;TD&gt;route&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;info&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;2ca011b5-ad34-4f32-a95c-78e8b5b1a270&lt;/TD&gt;&lt;TD&gt;GET&lt;/TD&gt;&lt;TD&gt;/orders/123456&lt;/TD&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;TD&gt;/order/{orderID}&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Is this what you wanted?&lt;/P&gt;&lt;P&gt;Here is a data emulation you can play with and compare with real data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw = "level=info request.elapsed=100 request.method=GET request.path=/orders/123456 request_id=2ca011b5-ad34-4f32-a95c-78e8b5b1a270 response.status=500"
| extract
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course, if for unknown reasons Splunk doesn't give you request_path, simply add an extract command and skip all the rex which is expensive.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 23:44:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684589#M233682</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-04-17T23:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new field based on regex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684720#M233716</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/33901"&gt;@yuanliu&lt;/a&gt;,&amp;nbsp;I explained why I couldn't use path directly, because it contains actual parameters.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, for the route /orders/{orderID}, the path could be:&lt;/P&gt;&lt;P&gt;/orders/123456&lt;/P&gt;&lt;P&gt;/orders/213123&lt;/P&gt;&lt;P&gt;/orders/435534&lt;/P&gt;&lt;P&gt;I want to analyze, for example, count of failed requests, or percentiles of call duration on this particular API route /orders/{orderID}.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course I can modify my service code to print the route pattern in log, but that is another way, i need to deploy new code to production environment.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 21:13:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684720#M233716</guid>
      <dc:creator>codewarrior</dc:creator>
      <dc:date>2024-04-18T21:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new field based on regex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684742#M233726</link>
      <description>&lt;P&gt;Have you tried my previous code?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval route = if(match(request_path, "^/orders/\d+"), "/order/{orderID}", null())&lt;/LI-CODE&gt;&lt;P&gt;This does exactly what you ask: create a new field named route that has a fixed pattern "/order/{orderID}". &amp;nbsp;Is there anything wrong with this?&lt;/P&gt;&lt;P&gt;In fact, because you really only care about first segment of the path - that fixed string "{orderID}" is just a decoration, the command could be simplified to slightly less expensive&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval route = "/" . mvindex(split(request_path, "/"), 1) . "/{orderID}"&lt;/LI-CODE&gt;&lt;P&gt;You can do whatever analysis against this field.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Apr 2024 04:25:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-new-field-based-on-regex/m-p/684742#M233726</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-04-19T04:25:57Z</dc:date>
    </item>
  </channel>
</rss>

