Splunk Search

Comparing values of fields without specifying field names

mahbs
Path Finder

Hi,

I have three fields, lets call them:

x = 6
y = 6
z = 0

What I want to be able to do is compare each of the fields with each other, and basically return the field that doesn't match, essentially, z is the only field that doesn't match so I would need that to be returned.

I don't want to specify the field names because I want to be able to reuse this query.

Is this possible?

Here's what I've done so far:

index=xxx sourcetype=xxx | fieldsummary | fields field,count | regex field="^([A-Z_]+)$" | where...

Thanks

Tags (1)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @mahbs,

Can you please try this search?

YOUR_SEARCH
| eval fields="",values="" 
| foreach "*" 
    [ eval fields = if("<<MATCHSTR>>"=="fields" OR "<<MATCHSTR>>"=="values",fields,fields.","."<<MATCHSTR>>") ,values = if("<<MATCHSTR>>"=="fields" OR "<<MATCHSTR>>"=="values",values,values.",".'<<FIELD>>') ] 
| eval fields=split(fields,","),values=split(values,",") 
| eval tmp=mvzip(fields,values) 
| stats count by tmp,_raw 
| eval fields=mvindex(split(tmp,","),0),values=mvindex(split(tmp,","),1) | where fields!=""
| stats values(fields) as fields values(_raw) as raw by values

Sample Search:

| makeresults 
| eval _raw = "x=6 y=6 z=0" 
| kv 
| fields - _time 
| eval fields="",values="" 
| foreach "*" 
    [ eval fields = if("<<MATCHSTR>>"=="fields" OR "<<MATCHSTR>>"=="values",fields,fields.","."<<MATCHSTR>>") ,values = if("<<MATCHSTR>>"=="fields" OR "<<MATCHSTR>>"=="values",values,values.",".'<<FIELD>>') ] 
| eval fields=split(fields,","),values=split(values,",") 
| eval tmp=mvzip(fields,values) 
| stats count by tmp,_raw 
| eval fields=mvindex(split(tmp,","),0),values=mvindex(split(tmp,","),1) | where fields!=""
| stats values(fields) as fields values(_raw) as raw by values

In this search, I'm trying to iterate all fields in raw and listing fields with different values.

Happy Splunking

0 Karma

woodcock
Esteemed Legend

Like this:

| makeresults 
| eval _raw = "x=6 y=6 z=0"
| kv
| fields - _time

| rename COMMENT AS "Everything above generates sample events; everything below is your solution"

| eval conclusion=case(x!=y AND x!=z AND y!=z, "AllDifferent",
                       x==y AND y==z, "AllTheSame",
                       x==y, "z",
                       x==z, "y",
                       y==z, "x",
                       true(), "INCONCEIVABLE")
0 Karma

niketn
Legend

@mahbs, once fieldsummary returns fields with event count, do you want to check all event counts and return fields which do not have event count matching any other field?

Following is run anywhere search based on data provided (A=6, B=6 and C=0 which returns field="C") :

| makeresults
| eval field="A", count=6
|  append 
    [| makeresults
| eval field="B", count=6]
|  append 
    [| makeresults
| eval field="C", count=0]
| stats count(count) as Counter values(field) as field by count
| search Counter=1

Following is run anywhere search based on another sample (A=6, B=0 and C=0 which returns field="A") :

| makeresults
| eval field="A", count=6
|  append 
    [| makeresults
| eval field="B", count=0]
|  append 
    [| makeresults
| eval field="C", count=0]
| stats count(count) as Counter values(field) as field by count
| search Counter=1

If the above result is as expected you can try the following run anywhere search with Splunk's _internal index in the base search:

index=_internal sourcetype=splunkd
| fieldsummary
| fields field count
| stats count(count) as Counter values(field) as field by count
| search Counter=1
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

damien_chillet
Builder

Are you willing to create a new field for each event that would hold the value of the field (for example "not_matching") that does not match?

0 Karma

mahbs
Path Finder

yep, I'm willing to do that

0 Karma

damien_chillet
Builder

I think it would be difficult if not impossible to achieve per event.
fieldsummary command gives info about fields for all the events returned by the event search.

0 Karma
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...