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!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...