Splunk Search

How to dynamically compare and come up with new fields

yoshilog
Explorer

Using the extract function, I can arrive with the below columns: 

 

yoshilog_0-1617169596797.png 

yoshilog_1-1617169642386.png

 

 

I need to compare the values, and come up with a new field like r1, r2, r3 which says whether it's same or not. 

I'm thinking of using eval function and then if statements to compare the two values but I'm not sure how to do it in such a way that will apply to all columns with titles beginning with "q" and "a".

I was thinking of using foreach loop but it seems that the foreach loop has very specific usecases that doesn't apply to mine.

The dilemma is that I need to do this dynamically, because it's possible that in other rows, there will be data reaching up to q5... q10... etc.

Is there a specific command for what I want to do? 

Labels (1)
Tags (3)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

Building on @manjunathmeti reply, to avoid having to deal with 'a' fields that are not part of your checking, you can use foreach like this, where <<FIELD>> is the number in the foreach comand

| makeresults 
| eval n=mvrange(1,51)
| eval Results="Yes,No"
| mvexpand n
| eval a{n}=mvindex(split(Results,","), random() % 2)
| eval q{n}=mvindex(split(Results,","), random() % 2)
| stats values(a*) as a* values(q*) as q*
| foreach 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [
  eval r<<FIELD>>=if(a<<FIELD>>=q<<FIELD>>, "Equal", "Not Equal") 
]

This creates 50 fields with a random a/q yes/no variant then compares each of them

 

 

View solution in original post

bowesmana
SplunkTrust
SplunkTrust

Building on @manjunathmeti reply, to avoid having to deal with 'a' fields that are not part of your checking, you can use foreach like this, where <<FIELD>> is the number in the foreach comand

| makeresults 
| eval n=mvrange(1,51)
| eval Results="Yes,No"
| mvexpand n
| eval a{n}=mvindex(split(Results,","), random() % 2)
| eval q{n}=mvindex(split(Results,","), random() % 2)
| stats values(a*) as a* values(q*) as q*
| foreach 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [
  eval r<<FIELD>>=if(a<<FIELD>>=q<<FIELD>>, "Equal", "Not Equal") 
]

This creates 50 fields with a random a/q yes/no variant then compares each of them

 

 

yoshilog
Explorer

Thank you for creating a dynamic example! It worked.

0 Karma

manjunathmeti
SplunkTrust
SplunkTrust

hi @yoshilog,

You can use foreach command with <<MATCHSTR>> instead of <<FIELD>>. You need to make sure that suffixes of fields a<SUFFIX> and q<SUFFIX> should be the same.
Try this:

| makeresults 
| eval afield1=4, afield2=2, afield3=8, afield4=10, qfield1=1, qfield2=2, qfield3=3, qfield4=10 
| foreach a* 
    [ eval r<<MATCHSTR>>=if(a<<MATCHSTR>>=q<<MATCHSTR>>, "EQUAL", "NOT EQUAL")]

 

If this reply helps you, a like would be appreciated.

yoshilog
Explorer

Thank you for sharing. It worked!

0 Karma
Get Updates on the Splunk Community!

Improve Your Security Posture

Watch NowImprove Your Security PostureCustomers are at the center of everything we do at Splunk and security ...

Maximize the Value from Microsoft Defender with Splunk

 Watch NowJoin Splunk and Sens Consulting for this Security Edition Tech TalkWho should attend:  Security ...

This Week's Community Digest - Splunk Community Happenings [6.27.22]

Get the latest news and updates from the Splunk Community here! News From Splunk Answers ✍️ Splunk Answers is ...