{"id":51532,"date":"2022-02-14T14:12:45","date_gmt":"2022-02-14T14:12:45","guid":{"rendered":"https:\/\/merikebi.warrenmyers.com\/?p=51532"},"modified":"2022-02-14T14:12:45","modified_gmt":"2022-02-14T14:12:45","slug":"answer-by-warren-for-extract-substring-from-splunk-string","status":"publish","type":"post","link":"https:\/\/merikebi.warrenmyers.com\/?p=51532","title":{"rendered":"Answer by warren for Extract substring from Splunk String"},"content":{"rendered":"<p>So long as you have at least three segments to a fully-qualified domain name, this should work (without using a regular expression)<\/p>\n<pre><code>index=ndx sourcetype=srctp host=*\n| makemv delim=&quot;.&quot; host\n| eval piece=substr(mvindex(host,3),1,4)\n...\n<\/code><\/pre>\n<p><a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/latest\/SearchReference\/makemv\" rel=\"nofollow noreferrer\"><code>makemv<\/code><\/a> converts a field into a multivalue field based on the <code>delim<\/code> you instruct it to use<\/p>\n<p>Then use <a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/latest\/SearchReference\/eval\" rel=\"nofollow noreferrer\"><code>eval<\/code><\/a> to grab the third item in the list using <a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/latest\/SearchReference\/MultivalueEvalFunctions#mvindex.28MVFIELD.2CSTARTINDEX.2C_ENDINDEX.29\" rel=\"nofollow noreferrer\"><code>mvindex<\/code><\/a>, trimming it with <a href=\"https:\/\/docs.splunk.com\/Documentation\/Splunk\/latest\/SearchReference\/TextFunctions#substr.28X.2CY.2CZ.29\" rel=\"nofollow noreferrer\"><code>substr<\/code><\/a><\/p>\n<p>If you <em>really<\/em> want to use a regular expression, <a href=\"https:\/\/regex101.com\/r\/GQIlFf\/1\" rel=\"nofollow noreferrer\">this<\/a> will do it (again, presuming you have at least three pieces to the FQDN):<\/p>\n<pre><code>index=ndx sourcetype=srctp host=*\n| rex field=host &quot;\\.[^\\.]+\\.(?&lt;piece&gt;[^\\.]{4})&quot;\n...\n<\/code><\/pre>\n<p>from User warren &#8211; Stack Overflow https:\/\/stackoverflow.com\/questions\/71109953\/extract-substring-from-splunk-string\/71113221#71113221<br \/>\nvia <a href=\"https:\/\/ifttt.com\/?ref=da&#038;site=wordpress\">IFTTT<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So long as you have at least three segments to a fully-qualified domain name, this should work (without using a regular expression) index=ndx sourcetype=srctp host=* | makemv delim=&quot;.&quot; host | eval piece=substr(mvindex(host,3),1,4) &#8230; makemv converts a field into a multivalue field based on the delim you instruct it to use Then use eval to grab &hellip;<br \/><a href=\"https:\/\/merikebi.warrenmyers.com\/?p=51532\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\">Continue reading <span class=\"screen-reader-text\">Answer by warren for Extract substring from Splunk String<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[991],"keyring_services":[],"class_list":["post-51532","post","type-post","status-publish","format-standard","hentry","category-blih","tag-stackexchange"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/51532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51532"}],"version-history":[{"count":1,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/51532\/revisions"}],"predecessor-version":[{"id":51533,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=\/wp\/v2\/posts\/51532\/revisions\/51533"}],"wp:attachment":[{"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51532"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/merikebi.warrenmyers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fkeyring_services&post=51532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}