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!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...