<?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 How can I list a single attribute and sort based on its value? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-list-a-single-attribute-and-sort-based-on-its-value/m-p/592973#M206388</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have documents similar to the one below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;request_id: 12345
   revision: 123
   other_field: stuff
   my_precious: { [-]
     1648665400.774453: { [-]
       keys: [ [-]
         key:key1,
         size: 329
       ]
       op: operation_1
     }
     1648665400.7817056: { [-]
       keys: [ [-]
         key:key2,
         size: 785
       ]
       op: operation_2
     }
     1648665400.7847242: { [-]
       keys: [ [-]
         key:key4,
         size: 632
       ]
       op: operation_1
     }
     1648665400.7886434: { [-]
     	keys: [ [-]
         key:key5,
         size: 1938
       ]
       op: operation_3
     }
     1648665400.7932374: { [-]
     	keys: [ [-]
         key:key3,
         size: 23
       ]
       op: operation_2
     }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I currently have a query to get the frequency of a certain key, but how can I display the "size" information along side with it?&lt;/P&gt;&lt;P&gt;My query right now is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;rex (?&amp;lt;keys&amp;gt;"(?&amp;lt;=key:).*?(?=,)") |stats count by keys | sort -count | head 10&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which displays the keys with the highest number of count, but it doesn't show the key's associated "size".&lt;/P&gt;&lt;P&gt;Can't quite figure this out...any help is appreciated!&lt;/P&gt;</description>
    <pubDate>Thu, 07 Apr 2022 19:15:54 GMT</pubDate>
    <dc:creator>aj_54321</dc:creator>
    <dc:date>2022-04-07T19:15:54Z</dc:date>
    <item>
      <title>How can I list a single attribute and sort based on its value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-list-a-single-attribute-and-sort-based-on-its-value/m-p/592973#M206388</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have documents similar to the one below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;request_id: 12345
   revision: 123
   other_field: stuff
   my_precious: { [-]
     1648665400.774453: { [-]
       keys: [ [-]
         key:key1,
         size: 329
       ]
       op: operation_1
     }
     1648665400.7817056: { [-]
       keys: [ [-]
         key:key2,
         size: 785
       ]
       op: operation_2
     }
     1648665400.7847242: { [-]
       keys: [ [-]
         key:key4,
         size: 632
       ]
       op: operation_1
     }
     1648665400.7886434: { [-]
     	keys: [ [-]
         key:key5,
         size: 1938
       ]
       op: operation_3
     }
     1648665400.7932374: { [-]
     	keys: [ [-]
         key:key3,
         size: 23
       ]
       op: operation_2
     }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I currently have a query to get the frequency of a certain key, but how can I display the "size" information along side with it?&lt;/P&gt;&lt;P&gt;My query right now is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;rex (?&amp;lt;keys&amp;gt;"(?&amp;lt;=key:).*?(?=,)") |stats count by keys | sort -count | head 10&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which displays the keys with the highest number of count, but it doesn't show the key's associated "size".&lt;/P&gt;&lt;P&gt;Can't quite figure this out...any help is appreciated!&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 19:15:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-list-a-single-attribute-and-sort-based-on-its-value/m-p/592973#M206388</guid>
      <dc:creator>aj_54321</dc:creator>
      <dc:date>2022-04-07T19:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: How can I list a single attribute and sort based on its value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-list-a-single-attribute-and-sort-based-on-its-value/m-p/592981#M206392</link>
      <description>&lt;P&gt;When Splunk processes the events in the search pipeline at every step you have only the results of previous step. Splunk "forgets" what it had before. So if you do "stats count by something", you have only a summarized table for further processing. No original events.&lt;/P&gt;&lt;P&gt;Anyway, I'm not sure what you want since you're counting, then talking about single value of size. Something doesn't fit here.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 19:45:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-list-a-single-attribute-and-sort-based-on-its-value/m-p/592981#M206392</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2022-04-07T19:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: How can I list a single attribute and sort based on its value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-list-a-single-attribute-and-sort-based-on-its-value/m-p/593022#M206419</link>
      <description>&lt;P&gt;Your data looks like conformant JSON, so you should process it as such. &amp;nbsp;Code will be much more maintainable if you use builtin functions. (This question is very similar to&amp;nbsp;&lt;A href="https://community.splunk.com/t5/Splunk-Search/How-to-create-a-table-using-SPATH-usage-on-simple-JSON" target="_blank" rel="noopener"&gt;https://community.splunk.com/t5/Splunk-Search/How-to-create-a-table-using-SPATH-usage-on-simple-JSON&lt;/A&gt;)&lt;/P&gt;&lt;P&gt;The way you displayed the sample data, Splunk has already extracted fields like&amp;nbsp;my_precious.1648665400.774453.keys{}.key,&amp;nbsp;my_precious.1648665400.774453.keys{}.size,&amp;nbsp;my_precious.1648665400.774453.op, etc., to the effect of (If not, its very easy to use&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath" target="_blank" rel="noopener"&gt;spath&lt;/A&gt;&amp;nbsp;to extract.)&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;TABLE width="1550px"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="191.90625px" height="25px"&gt;_raw&lt;/TD&gt;&lt;TD width="340.484375px" height="25px"&gt;my_precious.1648665400.774453.keys{}.key&lt;/TD&gt;&lt;TD width="343.71875px" height="25px"&gt;my_precious.1648665400.774453.keys{}.size&lt;/TD&gt;&lt;TD width="286.078125px" height="25px"&gt;my_precious.1648665400.774453.op&lt;/TD&gt;&lt;TD width="350.078125px" height="25px"&gt;my_precious.1648665400.7817056.keys{}.key&lt;/TD&gt;&lt;TD width="40px"&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="191.90625px" height="531px"&gt;{ "request_id": "12345", "revision": "123", "other_field": "stuff", "my_precious": { "1648665400.774453": { "keys": [ { "key": "key1" }, { "size": "329" } ], "op": "operation_1" }, "1648665400.7817056": { "keys": [ { "key": "key2" }, { "size": "785" } ], "op": "operation_2" }, "1648665400.7847242": { "keys": [ { "key": "key4" }, { "size": "632" } ], "op": "operation_1" }, "1648665400.7886434": { "keys": [ { "key": "key5" }, { "size": "1938" } ], "op": "operation_3" }, "1648665400.7932374": { "keys": [ { "key": "key3" }, { "size": "23" } ], "op": "operation_2" } } }&lt;/TD&gt;&lt;TD width="340.484375px" height="531px"&gt;key1&lt;/TD&gt;&lt;TD width="343.71875px" height="531px"&gt;329&lt;/TD&gt;&lt;TD width="286.078125px" height="531px"&gt;operation_1&lt;/TD&gt;&lt;TD width="350.078125px" height="531px"&gt;key2&lt;/TD&gt;&lt;TD width="40px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Like the other solution, we use a combination of foreach, join, mvmap, mvexpand, split, and so on to get key and corresponding size into respective fields.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rename my_precious.*.*.keys{}.* as key_*_*_*, my_precious.*.*.op as key_*_*_op ``` make names friendly ```
| foreach key_*_key
    [eval keys = mvappend(keys, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; . "|" . key_&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;_size . "|" . key_&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;_op)] ``` zip up key, size, and op into one string ```
| mvexpand keys ``` make each key its own event so we can apply stats easily
| eval keys = mvmap(keys, split(keys, "|")) ``` untie keys so we can dereference attributes ```
| eval key = mvindex(keys, 0), size = mvindex(keys, 1), op = mvindex(keys, 2)
| stats count by key size&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your sample data will give&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;key&lt;/TD&gt;&lt;TD&gt;size&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;key1&lt;/TD&gt;&lt;TD&gt;329&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;key2&lt;/TD&gt;&lt;TD&gt;785&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;key3&lt;/TD&gt;&lt;TD&gt;23&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;key4&lt;/TD&gt;&lt;TD&gt;632&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;key5&lt;/TD&gt;&lt;TD&gt;1938&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Of course, all count is 1 in the sample. &amp;nbsp;You didn't ask about op, but I thought you might be interested at some point so that's also taken care of.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 23:35:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-list-a-single-attribute-and-sort-based-on-its-value/m-p/593022#M206419</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2022-04-07T23:35:10Z</dc:date>
    </item>
  </channel>
</rss>

