Splunk Search

String Comparison with Foreach

zacksoft
Contributor

I have a lookup table from a csv that looks like this

name exam1 exam2 exam3
john good bad bad
peter bad bad best
ken best bad bad

and the list continues with almost 100 rows.
I want to know whether there is a 'best' under each exam.
So, I am thinking to implement foreach , which should give us a result something like this,

exam1 OK
exam2 NO
exam3 OK
(because for exam1 and exam3 there was atleast someone who performed 'best' but for exam2 no one has 'best'

This is what I tried

| inputlookup exam.csv 
| foreach * [eval final = if(<<FIELD>>=="best","OK","NO")]

But I can't make the command work. 😞

Tags (1)
0 Karma
1 Solution

vnravikumar
Champion

Hi

Give a try

| inputlookup sample.csv 
| stats list(exam1) as exam1,list(exam2) as exam2,list(exam3) as exam3 
| eval exam1 = if(tostring((mvfind(exam1,"best")))!="Null","OK","NO"), exam2 = if(tostring((mvfind(exam2,"best")))!="Null","OK","NO"), exam3 = if(tostring((mvfind(exam3,"best")))!="Null","OK","NO") 
| transpose 
| rename column as Exams,"row 1" as Results

OR

| inputlookup sample.csv 
| transpose column_name=Exams 
| search Exams != name 
| eval final = 'row 1'.",".'row 2' .",". 'row 3' 
| makemv delim="," final 
| eval final= if(tostring((mvfind(final,"best")))!="Null","OK","NO") 
| table Exams, final

View solution in original post

0 Karma

vnravikumar
Champion

Hi

Give a try

| inputlookup sample.csv 
| stats list(exam1) as exam1,list(exam2) as exam2,list(exam3) as exam3 
| eval exam1 = if(tostring((mvfind(exam1,"best")))!="Null","OK","NO"), exam2 = if(tostring((mvfind(exam2,"best")))!="Null","OK","NO"), exam3 = if(tostring((mvfind(exam3,"best")))!="Null","OK","NO") 
| transpose 
| rename column as Exams,"row 1" as Results

OR

| inputlookup sample.csv 
| transpose column_name=Exams 
| search Exams != name 
| eval final = 'row 1'.",".'row 2' .",". 'row 3' 
| makemv delim="," final 
| eval final= if(tostring((mvfind(final,"best")))!="Null","OK","NO") 
| table Exams, final
0 Karma
Get Updates on the Splunk Community!

Index This | What is broken 80% of the time by February?

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

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...

Splunk MCP & Agentic AI: Machine Data Without Limits

Discover how the Splunk Model Context Protocol (MCP) Server can revolutionize the way your organization uses ...