<?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 a regex to display all the hostnames? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400517#M116044</link>
    <description>&lt;P&gt;This should work:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| rex field=_raw "\/(?&amp;lt;hostname&amp;gt;[^_\/]+)[\w\.]+$"
| stats count by hostname
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If the strings like this &lt;CODE&gt;/log/web/output/sat1svmdb1210_0511_kernel.log&lt;/CODE&gt; are already being extracted into a field like &lt;CODE&gt;path&lt;/CODE&gt;, then you could make the search more efficient by specifying that field:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| rex field=path "\/(?&amp;lt;hostname&amp;gt;[^_\/]+)[\w\.]+$"
| stats count by hostname
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 16 May 2018 15:47:52 GMT</pubDate>
    <dc:creator>elliotproebstel</dc:creator>
    <dc:date>2018-05-16T15:47:52Z</dc:date>
    <item>
      <title>How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400516#M116043</link>
      <description>&lt;P&gt;I have source as : /log/web/output/sat1svmdb1210_0511_kernel.log&lt;BR /&gt;
/log/web/output/sat2svmdb0100_7689_kernel.log&lt;/P&gt;

&lt;P&gt;I want to capture the hostname i.e. sat1svmdb1210 and sat2svmdb0100 in a field and display all the hostname. How can I do it?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 19:33:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400516#M116043</guid>
      <dc:creator>abhi04</dc:creator>
      <dc:date>2020-09-29T19:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400517#M116044</link>
      <description>&lt;P&gt;This should work:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| rex field=_raw "\/(?&amp;lt;hostname&amp;gt;[^_\/]+)[\w\.]+$"
| stats count by hostname
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If the strings like this &lt;CODE&gt;/log/web/output/sat1svmdb1210_0511_kernel.log&lt;/CODE&gt; are already being extracted into a field like &lt;CODE&gt;path&lt;/CODE&gt;, then you could make the search more efficient by specifying that field:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| rex field=path "\/(?&amp;lt;hostname&amp;gt;[^_\/]+)[\w\.]+$"
| stats count by hostname
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 May 2018 15:47:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400517#M116044</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-05-16T15:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400518#M116045</link>
      <description>&lt;P&gt;Can you please explain the logic if that's possible?&lt;/P&gt;</description>
      <pubDate>Wed, 16 May 2018 16:06:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400518#M116045</guid>
      <dc:creator>abhi04</dc:creator>
      <dc:date>2018-05-16T16:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400519#M116046</link>
      <description>&lt;P&gt;Absolutely. The &lt;CODE&gt;rex&lt;/CODE&gt; command is looking at either the full event data (in the first example, where it looks at &lt;CODE&gt;field=_raw&lt;/CODE&gt;) or at the particular field (in the second example, where it looks at &lt;CODE&gt;field=path&lt;/CODE&gt;). Within that, it is looking to extract a field called &lt;CODE&gt;hostname&lt;/CODE&gt; by matching a regular expression that matches &lt;CODE&gt;"\/(?&amp;lt;hostname&amp;gt;[^_\/]+)[\w\.]+$"&lt;/CODE&gt;. Probably the best way to explain the regex would be to use regex101:&lt;BR /&gt;
&lt;A href="https://regex101.com/r/pGOUEK/1"&gt;https://regex101.com/r/pGOUEK/1&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;But in summary, it's looking for a &lt;CODE&gt;/&lt;/CODE&gt; character, then collecting all subsequent characters that are neither &lt;CODE&gt;_&lt;/CODE&gt; nor &lt;CODE&gt;/&lt;/CODE&gt;, followed by one or more characters that are either "word characters" (alphanumeric OR underscores) or periods - and anchoring all of this to the end of the field by using &lt;CODE&gt;$&lt;/CODE&gt;.  Sorry, I'm not very good at putting regexes into plain English!&lt;/P&gt;</description>
      <pubDate>Wed, 16 May 2018 17:10:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400519#M116046</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-05-16T17:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400520#M116047</link>
      <description>&lt;P&gt;As I typed all this out, I realized the first option might not work for you, as the path you're parsing might not be at the end of the event. Here's a fixed regex:&lt;BR /&gt;
&lt;A href="https://regex101.com/r/pGOUEK/2"&gt;https://regex101.com/r/pGOUEK/2&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;In Splunk that would be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| rex field=_raw "\/(?&amp;lt;hostname&amp;gt;[^_\/]+)[\w\.]+($|\s)"
| stats count by hostname
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 May 2018 17:12:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400520#M116047</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-05-16T17:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400521#M116048</link>
      <description>&lt;P&gt;@abhi04, if it is the default field &lt;CODE&gt;host&lt;/CODE&gt; that you need to have extracted from the &lt;CODE&gt;source log file&lt;/CODE&gt; name being monitored, you can Set &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/Data/Setadefaulthostforaninput#Dynamically_set_the_default_host_value"&gt;Default Host for File or Directory input using Regular Expression&lt;/A&gt; (either from Web UI or from &lt;CODE&gt;inputs.conf&lt;/CODE&gt; configuration file)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[monitor://log/web/output/*.log]
host_regex = ^.*\/([^_]+)\_[^_]+_kernel.log$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This would imply that host name will show up as default field and will not be required to be extracted during Search time. Of course, if &lt;CODE&gt;hostname&lt;/CODE&gt; is different from &lt;CODE&gt;host&lt;/CODE&gt; you would need to rely on Search Time Field Extraction (using rex command which can be saved as regular expression based Field Extraction using Interactive Field Extraction or props.conf).&lt;BR /&gt;
Use regex101.com to learn and test regular expressions with sample data. (It provides an step by step explanation of the extraction).&lt;/P&gt;</description>
      <pubDate>Wed, 16 May 2018 17:18:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400521#M116048</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-05-16T17:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400522#M116049</link>
      <description>&lt;P&gt;So the [^_/] will search for characters untill _ and / is found?&lt;BR /&gt;
If yes then why we are nearing /,only _ should be negated.please explain.&lt;/P&gt;</description>
      <pubDate>Wed, 16 May 2018 17:35:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400522#M116049</guid>
      <dc:creator>abhi04</dc:creator>
      <dc:date>2018-05-16T17:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400523#M116050</link>
      <description>&lt;P&gt;@abhi04, @elliotproebstel  has provided you with regex101 link i.e. &lt;A href="https://regex101.com/r/pGOUEK/2"&gt;https://regex101.com/r/pGOUEK/2&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;If you open the link on the right side the &lt;CODE&gt;EXPLANATION&lt;/CODE&gt; section give step by step details of pattern match of each individual character in the regular expression.&lt;/P&gt;

&lt;P&gt;Even if you are not familiar with Regular Expressions, you would notice in the bottom right there is a &lt;CODE&gt;QUICK REFERENCE&lt;/CODE&gt; with &lt;CODE&gt;Search Reference&lt;/CODE&gt; text bar where you can type in any character from Regular expression to see what they mean for example &lt;CODE&gt;[^_\/]&lt;/CODE&gt; &lt;CODE&gt;means a single character not present in the list _\/&lt;/CODE&gt;. With a plus sign &lt;CODE&gt;+&lt;/CODE&gt; that follows it means repeat until any character in the list is found.&lt;/P&gt;

&lt;P&gt;Also remember to use the &lt;CODE&gt;code&lt;/CODE&gt; button i.e. &lt;CODE&gt;101010&lt;/CODE&gt; or shortcut key &lt;CODE&gt;Ctrl+K&lt;/CODE&gt; before posting code/data on Splunk Answers so that special characters do not escape.&lt;/P&gt;</description>
      <pubDate>Wed, 16 May 2018 18:30:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400523#M116050</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-05-16T18:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400524#M116051</link>
      <description>&lt;P&gt;Thanks for the quick help.&lt;/P&gt;</description>
      <pubDate>Wed, 16 May 2018 18:41:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400524#M116051</guid>
      <dc:creator>abhi04</dc:creator>
      <dc:date>2018-05-16T18:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a regex to display all the hostnames?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400525#M116052</link>
      <description>&lt;P&gt;Anytime, do up vote the comments that helped &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 May 2018 18:44:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-regex-to-display-all-the-hostnames/m-p/400525#M116052</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-05-16T18:44:05Z</dc:date>
    </item>
  </channel>
</rss>

