Splunk Search

How to grab a different field in an event, based on other criteria

DATT
Path Finder

I have a table that looks like this

 

DayPercent
2024-11-01100
2024-11-0299.6
2024-11-0394.2
......
2024-12-0122.1
2024-12-0219.0

 

From this table I am calculating three fields: REMEDIATION_50, _80, and _100 using the following

 

|eval REMEDIATION_50 = if(PERCENTAGE <= 50, "x", "")

 

 

From this eval statement, I am going to have multiple rows where the _50, and _80 rows are marked, and some where both fields are marked.  I'm interested in isolating the DAY of the first time each of these milestones are hit.  I've yet to craft the right combination of stats, where, and evals that gets me what I want.

In the end, I'd like to get to this of sorts

Start50%80%100%
2024-11-012024-11-232024-12-02-

 

Any help would be appreciated, thanks!

Labels (2)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

Is this what you're after

| makeresults format=csv data="Day,Percent
2024-11-01,100
2024-11-02,99.6
2024-11-03,94.2
2024-12-01,22.1
2024-12-02,19.0"
| eval _time=strptime(Day, "%F")
| foreach 50 80 100 [ eval REMEDIATION_<<FIELD>> = if(Percent <= <<FIELD>>, 1,null())]
| stats earliest_time(_time) as Start earliest_time(REMEDIATION_*) as r_*
| foreach r_* [ eval <<MATCHSTR>>%=<<FIELD>> | fields - <<FIELD>> ]
| foreach * [ eval "<<FIELD>>"=strftime('<<FIELD>>', "%F") ]

View solution in original post

0 Karma

bowesmana
SplunkTrust
SplunkTrust

Is this what you're after

| makeresults format=csv data="Day,Percent
2024-11-01,100
2024-11-02,99.6
2024-11-03,94.2
2024-12-01,22.1
2024-12-02,19.0"
| eval _time=strptime(Day, "%F")
| foreach 50 80 100 [ eval REMEDIATION_<<FIELD>> = if(Percent <= <<FIELD>>, 1,null())]
| stats earliest_time(_time) as Start earliest_time(REMEDIATION_*) as r_*
| foreach r_* [ eval <<MATCHSTR>>%=<<FIELD>> | fields - <<FIELD>> ]
| foreach * [ eval "<<FIELD>>"=strftime('<<FIELD>>', "%F") ]
0 Karma

DATT
Path Finder

This gets me pretty close to what I need.  I modified it slightly to get to the data I need:

| makeresults format=csv data="Day,Percent
2024-11-01,100
2024-11-02,99.6
2024-11-03,94.2
2024-11-04, 79.9
2024-11-30, 49.9
2024-12-01,22.1
2024-12-02,19.0" 

| eval _time=strptime(Day, "%F") 
| foreach 50 80 100
    [ eval REMAINING = 100 - <<FIELD>> 
    | eval REMEDIATION_<<FIELD>> = if(Percent <= REMAINING, 1, null())] 
   
| stats earliest_time(_time) as Start earliest_time(REMEDIATION_*) as r_*

I'll need to figure out a way to get the 100% field to show up after the stats command but I know I can do that in a brute force manner if necessary.

 

I haven't seen foreach before so thank you for such a concise, relevant example. 

bowesmana
SplunkTrust
SplunkTrust

foreach is immensely powerful and leads you to a place where in your SPL you can use good field naming conventions to create concise, if a little more obtuse, logic. Here it's using numbers, but you typically use it with fields and then wildcards then a good naming strategy become important as it allows you to handle unknown field names.

0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  &#x1f680; Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...