Splunk Search

How do you indirectly access a field value?

doton
New Member

In the following query, I want to use the value of b as a field:

| makeresults 
| eval a=1
| eval b="a"
| eval c=some_operation_based_on(b)

I want c to be equal to the value of a i.e. 1 in this example. I have tried using foreach and {field_name} but I wasn't able to achieve the result I want.

Tags (3)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@doton
Can you please try this?

| makeresults 
| eval a=1 
| eval b="a" 
| eval c="" 
| foreach * 
    [ eval c=if("<<FIELD>>"==b,<<FIELD>>,c)]

You can do any operations as per you requirement,

0 Karma

doton
New Member

@kamlesh_vaghela, Thanks it works, but I don't understand how it works. If I understand correctly, <<FIELD>> will be replaced by filed names as foreach loops through them. Thus when the field is b the the eval expression should be equal to eval c=if("b"==b,b,c) and b is equal to "a" so c should be equal to "a". In the next iteration of the loop it will became eval c=if("c"==b,c,c) so it shouldn't change the value of c.
Can you please elaborate?

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@doton

In foreach, it will only assign a value in field c if the field name match with the value of c (which is contain the name of field) else it will assign it self's value ( means c will assign value of c only). This will not override the matched values. So it is basic if else concept.

🙂

0 Karma

doton
New Member

Thanks, I was missing this point

if the field name match with the value

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@doton

Great..
Please upvote and accept the answer to close this question.

0 Karma

harishalipaka
Motivator

@doton

can u try like this $result.b$

Thanks
Harish
0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...