<?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: Can Splunk do this with a Search Query....? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34389#M7389</link>
    <description>&lt;PRE&gt;
&lt;YOUR_SEARCH&gt;
| eval len=length(q)
| eventstats max(len) AS longest
| where len=longest
| stats count by q
&lt;/YOUR_SEARCH&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 14 Nov 2012 16:10:15 GMT</pubDate>
    <dc:creator>sowings</dc:creator>
    <dc:date>2012-11-14T16:10:15Z</dc:date>
    <item>
      <title>Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34382#M7382</link>
      <description>&lt;P&gt;I have a log that registers search queries with the following format:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  _time                     q       MAC
11/13/12 12:46:31.000 AM    CA      0000000B4BE8
11/13/12 12:46:31.200 AM    CARL        0000000B4BE8
11/13/12 12:46:31.250 AM    CARLIE      0000000B4BE8
11/13/12 12:46:31.255 AM    CARLIE B    0000000B4BE8
11/13/12 12:46:31.270 AM    CARLIE BR   0000000B4BE8
11/13/12 12:46:32.100 AM    CHAR        0000000B4BE8
11/13/12 12:46:32.150 AM    CHARL       0000000B4BE8
11/13/12 12:46:32.155 AM    CHARLI      0000000B4BE8
11/13/12 12:46:32.223 AM    CHARLIE     0000000B4BE8
11/13/12 12:46:32.354 AM    CHARLIE B   0000000B4BE8
11/13/12 12:46:33.400 AM    CHARLIE BR  0000000B4BE8
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The result set I need should be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;MAC      count      q
0000000B4BE8 5     CARLIE BR 
0000000B4BE8 6     CHARLIE BR
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The recommended query found in below answer is the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search construct|
| stats count by q, MAC
| eval len=length(q)
| eventstats max(len) AS longest by MAC| where len=longest
| table MAC q count
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The query does not returned the distinct longest q. If this query is ran against the sample log presented before the result set is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;MAC      count    q
0000000B4BE8 6       CHARLIE BR
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It should be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;MAC      count      q
0000000B4BE8 5     CARLIE BR 
0000000B4BE8 6     CHARLIE BR
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any ideas? How to fix it.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Lp&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 15:16:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34382#M7382</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T15:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34383#M7383</link>
      <description>&lt;P&gt;Sure it can. Once you have the log in splunk, find it by using a search for that log, pipe it to stats, and away you go!&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;search_for_log | stats count by q&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 15:34:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34383#M7383</guid>
      <dc:creator>alacercogitatus</dc:creator>
      <dc:date>2012-11-14T15:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34384#M7384</link>
      <description>&lt;P&gt;Yes; you'll want to write a field extraction to either capture the "A" "AL" "ALF" "ALL" values as their own field, or include them as part of the "q" field, then you can &lt;CODE&gt;| stats count by &amp;lt;field&amp;gt;&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;If you capture it as q, the command would look like &lt;CODE&gt;| stats count by q&lt;/CODE&gt;; if you kept the contents of q the same (right now, auto-KV extraction would extract it as "UP"), but created a new field (e.g. "otherfield") for the A- values, your command would be &lt;CODE&gt;| stats count by q, otherfield&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 15:36:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34384#M7384</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T15:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34385#M7385</link>
      <description>&lt;P&gt;No it does not do the trick. Look at the question again.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:00:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34385#M7385</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T16:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34386#M7386</link>
      <description>&lt;P&gt;Thanks but it does not do the trick to select the longest q.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:05:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34386#M7386</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T16:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34387#M7387</link>
      <description>&lt;P&gt;Well then taking into account the nonquoted values for q (auto kv breaks here) add some rex to your search.&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;search_for_log| rex field=_raw "q=(?&amp;lt;q_extracted&amp;gt;UP\s+[\w]+)\s+MAC)"|stats count by q_extracted&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:07:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34387#M7387</guid>
      <dc:creator>alacercogitatus</dc:creator>
      <dc:date>2012-11-14T16:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34388#M7388</link>
      <description>&lt;P&gt;Then perhaps you should have been more explicit in your question.&lt;/P&gt;

&lt;P&gt;I'd tackle that by collecting q as a field, use eval to set &lt;CODE&gt;| eval len=length(q)&lt;/CODE&gt;, then &lt;CODE&gt;| eventstats max(len) AS longest&lt;/CODE&gt; next another search to filter to just those that have that length: &lt;CODE&gt;| where len=longest&lt;/CODE&gt; and finally a stats command to get your final output: &lt;CODE&gt;| stats count by q&lt;/CODE&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:09:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34388#M7388</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T16:09:19Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34389#M7389</link>
      <description>&lt;PRE&gt;
&lt;YOUR_SEARCH&gt;
| eval len=length(q)
| eventstats max(len) AS longest
| where len=longest
| stats count by q
&lt;/YOUR_SEARCH&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:10:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34389#M7389</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T16:10:15Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34390#M7390</link>
      <description>&lt;P&gt;thanks let me test. It might do the trick.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:12:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34390#M7390</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T16:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34391#M7391</link>
      <description>&lt;P&gt;It tried. It does not do the trick. It will always select the longest string. If you add |stats count by q MAC you will get the longest by MAC but not the distinct longest by MAC.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:20:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34391#M7391</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T16:20:00Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34392#M7392</link>
      <description>&lt;P&gt;In every instance of your content sample, MAC is "1". Can you update that with actual sample data, and a clearer description of what you're after?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 16:37:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34392#M7392</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T16:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34393#M7393</link>
      <description>&lt;P&gt;I updated the question. thanks for your help.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 18:04:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34393#M7393</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T18:04:54Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34394#M7394</link>
      <description>&lt;P&gt;Out of the box, Splunk will extract q having these values: UP SON HEL HELLO. I've had to use a regex on this sample to extract the full (with spaces) q field.&lt;/P&gt;

&lt;PRE&gt;
rex "q\=(?&lt;NEW_Q&gt;[\w\s]+)(?:\s+MAC=)"
&lt;/NEW_Q&gt;&lt;/PRE&gt;

&lt;P&gt;Since you wanted it broken out by MAC, I also had to add this to my eventstats command, and the final stats call.&lt;/P&gt;

&lt;PRE&gt;
&lt;YOUR_SEARCH&gt;
| eval len=length(q)
| eventstats max(len) AS longest by MAC
| where len=longest
| stats count by MAC, q
| table MAC q count
&lt;/YOUR_SEARCH&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Nov 2012 18:39:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34394#M7394</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T18:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34395#M7395</link>
      <description>&lt;P&gt;It does not find the distinct searches. It just grabs the longest q. any idea?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 19:09:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34395#M7395</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T19:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34396#M7396</link>
      <description>&lt;P&gt;You &lt;STRONG&gt;asked&lt;/STRONG&gt; for the longest q. The search string I've posted in the comment above (combined with the &lt;CODE&gt;rex&lt;/CODE&gt; statement to capture all of the "q" field) works and produces the output you've asked for in the original question.&lt;/P&gt;

&lt;P&gt;If you're having trouble with it, post the results you get from the above search, so that we can figure out what's going wrong.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 19:22:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34396#M7396</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T19:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34397#M7397</link>
      <description>&lt;P&gt;I am already extracting q and MAC both are extracted automatically by Splunk. Do I need the rex command?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 19:25:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34397#M7397</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T19:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34398#M7398</link>
      <description>&lt;P&gt;If you're extracting q automatically by Splunk, you're using the automatic key=value extraction. Because of the whitespace in the q field, Splunk will see that as a separate word, and not as part of the value of "q". Therefore, &lt;EM&gt;yes&lt;/EM&gt; you will need the rex command I've provided above.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 19:34:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34398#M7398</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T19:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34399#M7399</link>
      <description>&lt;P&gt;I updated the question. It did the job. thanks I learned from your example. One more question. Should I expect any search limitations with this query?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 19:55:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34399#M7399</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T19:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34400#M7400</link>
      <description>&lt;P&gt;It is quite slow... Any way to improve the query?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 19:57:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34400#M7400</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2012-11-14T19:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Can Splunk do this with a Search Query....?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34401#M7401</link>
      <description>&lt;P&gt;Search limitations? As in performance? No, I wouldn't think so. Eventstats can be a little heavy, but given what you're doing, it's necessary, so....&lt;/P&gt;

&lt;P&gt;If you find that there are performance problems with the search, you could perform the stats count by q, MAC &lt;EM&gt;first&lt;/EM&gt;, then do the eval, eventstats, where, etc, after.&lt;/P&gt;

&lt;P&gt;Stats will reduce the overall number of rows by performing aggregation, and discarding detail (in this case, time stamps go away, but you get the count), so the &lt;EM&gt;next&lt;/EM&gt; search commands have smaller data sets to work with, and are therefore a bit faster.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Nov 2012 20:03:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-Splunk-do-this-with-a-Search-Query/m-p/34401#M7401</guid>
      <dc:creator>sowings</dc:creator>
      <dc:date>2012-11-14T20:03:43Z</dc:date>
    </item>
  </channel>
</rss>

