Splunk Search

correlate data without subsearch or transaction

stwong
Communicator

Hi,

I got data that have some fields missing in some events, e.g.

field1    field2    field3    field4   field5
  A                            val1
            B        val2
  A         B                             val3
  C                            val5
            D        val4
  C         D                             val6  

and want to group by either field1 or field2 to make output like this:

field1    field2    field3    field4   field5
  A          B        val2      val1    val3
  C          D        val4      val5    val6

I tried to avoid join, subsearch, or transaction. Is it possible?
Sorry for newbie question.

Thanks and rgds
/st wong

Tags (1)
0 Karma
1 Solution

elliotproebstel
Champion

I'd approach this by using eventstats before stats, given the data you presented.

First, cross-apply the values from field1 and field2:

| eventstats values(field1) AS field1 BY field2

This should take your first table and make it look like this:

 field1    field2    field3    field4   field5
   A                            val1
   A         B        val2
   A         B                             val3
   C                            val5
   C         D        val4
   C         D                             val6  

Now every row in the table has a value for field1, making it possible to use stats to populate the table:

| stats values(field2) AS field2 values(field3) AS field3 values(field4) AS field4 values(field5) AS field5 BY field1

If that doesn't work, it means the table likely has some instances where there is no value for field1, like maybe this:

  field1    field2    field3    field4   field5
    A                            val1
    A         B        val2
    A         B                             val3
    C                            val5
    C         D        val4
    C         D                             val6
              E                             val7
              E                  val8

If your data has events like that, I can help you adjust your search. It'll be a little more complex, but it's still doable.

View solution in original post

elliotproebstel
Champion

I'd approach this by using eventstats before stats, given the data you presented.

First, cross-apply the values from field1 and field2:

| eventstats values(field1) AS field1 BY field2

This should take your first table and make it look like this:

 field1    field2    field3    field4   field5
   A                            val1
   A         B        val2
   A         B                             val3
   C                            val5
   C         D        val4
   C         D                             val6  

Now every row in the table has a value for field1, making it possible to use stats to populate the table:

| stats values(field2) AS field2 values(field3) AS field3 values(field4) AS field4 values(field5) AS field5 BY field1

If that doesn't work, it means the table likely has some instances where there is no value for field1, like maybe this:

  field1    field2    field3    field4   field5
    A                            val1
    A         B        val2
    A         B                             val3
    C                            val5
    C         D        val4
    C         D                             val6
              E                             val7
              E                  val8

If your data has events like that, I can help you adjust your search. It'll be a little more complex, but it's still doable.

stwong
Communicator

Hi, it works perfectly. Thanks a lot.

0 Karma

p_gurav
Champion

Did you tryappendcols? Like:

..... | stats values(field3) AS field3 values(field4) AS field4 values(field5) AS field5  by field1 | appendcols [search index="abc_new" sourcetype="csv" | stats values(field3) AS field3 values(field4) AS field4 values(field5) AS field5  by field2] |table field1 field2 field3 field4 field5
0 Karma

stwong
Communicator

Thanks, but can appendcols do something like following?

..| stats values(field3) as field3, values(field4) as field4, values(field5) as field5 by "either field1 or field2" ?

and also hope to avoid subsearches.

Thanks and rgds

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Note: This post outlines a proposed architecture and serves as an interest check. If we secure commitments ...