<?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 Group CSV data results by two or more matching values? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25047#M96624</link>
    <description>&lt;P&gt;I'm indexing a CSV that appears like the following in its raw form:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Filenum,string
1,abc
2,defg
2,abc
3,xyz
3,abc
1,xyz
7,uiop
7,abc
4,defg
5,qazwsx
6,qazwsx
1,uiop
4,abc
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;etc..&lt;/P&gt;

&lt;P&gt;In Splunk both "Filenum" and "String" are correctly being extracted as field names.&lt;/P&gt;

&lt;P&gt;I'd like to spit out a table that automatically groups Filenums with two or more matching Strings.&lt;/P&gt;

&lt;P&gt;For example, Filenum 1 &amp;amp; 3 can be grouped together since they both have Strings abc &amp;amp; xyz.&lt;/P&gt;

&lt;P&gt;Sample desired output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Filenum     1, 3     abc, xyz
Filenum     1, 7     abc, uiop
Filenum     2, 4     abc, defg
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any ideas?&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 11 Apr 2012 20:03:37 GMT</pubDate>
    <dc:creator>jtsplunk</dc:creator>
    <dc:date>2012-04-11T20:03:37Z</dc:date>
    <item>
      <title>Group CSV data results by two or more matching values?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25047#M96624</link>
      <description>&lt;P&gt;I'm indexing a CSV that appears like the following in its raw form:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Filenum,string
1,abc
2,defg
2,abc
3,xyz
3,abc
1,xyz
7,uiop
7,abc
4,defg
5,qazwsx
6,qazwsx
1,uiop
4,abc
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;etc..&lt;/P&gt;

&lt;P&gt;In Splunk both "Filenum" and "String" are correctly being extracted as field names.&lt;/P&gt;

&lt;P&gt;I'd like to spit out a table that automatically groups Filenums with two or more matching Strings.&lt;/P&gt;

&lt;P&gt;For example, Filenum 1 &amp;amp; 3 can be grouped together since they both have Strings abc &amp;amp; xyz.&lt;/P&gt;

&lt;P&gt;Sample desired output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Filenum     1, 3     abc, xyz
Filenum     1, 7     abc, uiop
Filenum     2, 4     abc, defg
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any ideas?&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 11 Apr 2012 20:03:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25047#M96624</guid>
      <dc:creator>jtsplunk</dc:creator>
      <dc:date>2012-04-11T20:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: Group CSV data results by two or more matching values?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25048#M96625</link>
      <description>&lt;P&gt;While &lt;CODE&gt;contingency&lt;/CODE&gt; is interesting, here's a first pass at a solution that gives what you ask for: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats values(String) as Strings by Filenum
| eval lhs=trim(Filenum)."*".trim(mvjoin(Strings,";"))
| streamstats current=f values(lhs) as rhs | mvexpand rhs
| rex field=rhs "^(?&amp;lt;OtherFilenum&amp;gt;\d+)[*](?&amp;lt;OtherStrings&amp;gt;.+)$"
| makemv delim=";" OtherStrings | fields - lhs rhs
| mvexpand Strings | mvexpand OtherStrings | where Strings==OtherStrings
| stats values(Strings) as Strings by Filenum, OtherFilenum | where mvcount(Strings) &amp;gt;=2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What just happened here? &lt;/P&gt;

&lt;P&gt;Well assuming that every row in the csv is a separate event with the case sensitive field names &lt;CODE&gt;Filenum&lt;/CODE&gt; and &lt;CODE&gt;String&lt;/CODE&gt; , the first line we use &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/stats"&gt;stats&lt;/A&gt; to gather every String for each Filenum together into a multivalued field called &lt;CODE&gt;Strings&lt;/CODE&gt;.  Now assuming that &lt;CODE&gt;;&lt;/CODE&gt; does not appear in any string, and Filenum are numeric values, we turn each row into a single string form of the File Number * and all strings separated by semicolons.  (Obviously delimiters will have to change if the assumption is not correct. ) By doing this, we can use &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/streamstats"&gt;streamstats&lt;/A&gt; and &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/mvexpand"&gt;mvexpand&lt;/A&gt; to get us all of the comparisons we need to perform (2 to 1; 3 to 2, and 1, 4 to 3, 2, and 1, etc.). The next two lines we use &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/rex"&gt;rex&lt;/A&gt; and &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/makemv"&gt;makemv&lt;/A&gt; to parse the serialized form of the right side of the comparison back into the OtherFilenum and OtherStrings and since we no longer need the serialized forms we throw them away using &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/fields"&gt;fields&lt;/A&gt;. Using mvexpand again on both the &lt;CODE&gt;Strings&lt;/CODE&gt; and &lt;CODE&gt;OtherStrings&lt;/CODE&gt; we get every pairing of strings present in the left hand side and right hand side of the comparison. We then use &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/where"&gt;where&lt;/A&gt; to keep only those pairs that match, and then use stats again to gather them up by pairs of FileNums. Finally we use where again to keep only those that have at least 2 different strings match. &lt;/P&gt;

&lt;P&gt;There are likely optimizations that could be done here, but this should give you a decent start.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2015 19:28:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25048#M96625</guid>
      <dc:creator>acharlieh</dc:creator>
      <dc:date>2015-07-05T19:28:26Z</dc:date>
    </item>
    <item>
      <title>Re: Group CSV data results by two or more matching values?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25049#M96626</link>
      <description>&lt;P&gt;Bravo! I sunk about an hour of thinking and playing around with this before I realized that he was essentially asking for &lt;CODE&gt;contingency&lt;/CODE&gt; and decided that was good enough.  Part of that was spent figuring out a good way to mock the data:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=* | head 10 | streamstats current=f count AS serial | eval Filenum=serial | eval serialPlus1=serial+1 | eval serialPlus2=serial+2 | eval String= serial . "," . serialPlus1 . "," . serialPlus2 | table Filenum String | makemv delim="," String | mvexpand String | eval String = case(
Filenum=1 AND String=1, "abc", 
Filenum=1 AND String=2, "xyz", 
Filenum=1 AND String=3, "uiop", 
Filenum=2 AND String=2, "abc", 
Filenum=2 AND String=3, "defg", 
Filenum=3 AND String=3, "abc", 
Filenum=3 AND String=4, "xyz", 
Filenum=4 AND String=4, "abc", 
Filenum=4 AND String=5, "defg", 
Filenum=5 AND String=5, "qazwsx", 
Filenum=6 AND String=6, "qazwsx", 
Filenum=7 AND String=7, "abc", 
Filenum=7 AND String=8, "uiop")
| where isnotnull(String)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2015 21:34:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25049#M96626</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-07-05T21:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: Group CSV data results by two or more matching values?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25050#M96627</link>
      <description>&lt;P&gt;This isn't exactly what you asked for but, IMHO, is probably even better; try the &lt;CODE&gt;contingency&lt;/CODE&gt; command like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | contingency Filenum String
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This builds a table like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Filenum abc xyz uiop    qazwsx  defg    TOTAL
1            1      1       1         0       0       3
7            1      0       1         0       0       2
4            1      0       0         0       1       2
3            1      1       0         0       0       2
6            0      0       0         1       0       1
5            0      0       0         1       0       1
2            1      0       0         0       1       2
TOTAL       5      2       2         2       2      13
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2015 21:34:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25050#M96627</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-07-05T21:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Group CSV data results by two or more matching values?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25051#M96628</link>
      <description>&lt;P&gt;@woodcock Actually with the undocumented noop command in conjunction with &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/multikv"&gt;multikv&lt;/A&gt; mocking CSV data is quite easy... I simply did this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| noop | stats count | eval _raw="Filenum,String
 1,abc
 2,defg
 2,abc
 3,xyz
 3,abc
 1,xyz
 7,uiop
 7,abc
 4,defg
 5,qazwsx
 6,qazwsx
 1,uiop
 4,abc" | multikv | table Filenum, String
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2015 21:41:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Group-CSV-data-results-by-two-or-more-matching-values/m-p/25051#M96628</guid>
      <dc:creator>acharlieh</dc:creator>
      <dc:date>2015-07-05T21:41:44Z</dc:date>
    </item>
  </channel>
</rss>

