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!

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...