Splunk Search

How to access a field name using variable ?

sagrl
Explorer

My Splunk results are returning multiple fields including fields Sunday, Monday, Tuesday .... Saturday.

Now my requirement is if today is Sunday i want to access the value of field name Sunday, if today is Monday i need to access the value of Monday and so on..

Can someone help me in how to access the value of a field using variable ?

Tags (1)
0 Karma
1 Solution

somesoni2
Revered Legend

Without seeing your query or expected output, my suggestion would be this (the subsearch would return the current weekday value and pass it on to table command.:

your search giving fields for each weekday
| table [| gentimes start=-1 | eval search=strftime(now(),"%A") | table search ]

View solution in original post

woodcock
Esteemed Legend

I would do it like this:

| makeresults 
| eval date_wday=strftime(now(), "%A")
| map [search index="YouShouldAlwaysSpecifyAnIndex" sourcetype="AndSourcetypeToo" MyFieldName="$date_wday$"]
0 Karma

niketn
Legend

@sagrl, you can run a dummy search in your dashboard to get today's weekday as a token and then use the same in your other searches in the dashboard.

<search>
    <query>| makeresults
| eval weekDay=lower(strftime(_time,"%A"))
    </query>
    <progress>
       <set token="tokenWeekDay">$result.weekDay$</set>
    </progress>
</search>

Then use the token $tokenWeekDay$ in your other searches in the dashboard:

<single>
    <search>
        <query>| makeresults
    | eval tokenData="$tokenWeekDay$"
    | table tokenData
        </query>
    </search>
</single>

PS: I have changed the weekday to lower case using lower() function. In case your use case is to match this against Splunk's default extracted field date_wday, it should be lowercase. Result of strftime() time modifier%Awould result in Week Days like Sunday, Monday etc. As you have asked in your question. Hence, if you are search for same casing in weekday values, then you do not require lower() function, juststrftime()` should work.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

somesoni2
Revered Legend

Without seeing your query or expected output, my suggestion would be this (the subsearch would return the current weekday value and pass it on to table command.:

your search giving fields for each weekday
| table [| gentimes start=-1 | eval search=strftime(now(),"%A") | table search ]
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Introduction to Splunk AI

How are you using AI in Splunk? Whether you see AI as a threat or opportunity, AI is here to stay. Lucky for ...

Splunk + ThousandEyes: Correlate frontend, app, and network data to troubleshoot ...

Are you tired of troubleshooting delays caused by siloed frontend, application, and network data? We've got a ...

Maximizing the Value of Splunk ES 8.x

Splunk Enterprise Security (ES) continues to be a leader in the Gartner Magic Quadrant, reflecting its pivotal ...