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
Champion

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
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!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...