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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...