Splunk Search

Question on comparing 2 columns

jiaqya
Builder

i have 2 columns as below. Please see if you have a way to do this .. thanks..

Requirement is
if col1 = col2 , col1 record stays.
if col1 is not equal to col2, then col2 record stays , col1 record to be removed.

any way of doing this.

col1 col2
1 1
2 2.1
3 3

Tags (1)
0 Karma
1 Solution

vnravikumar
Champion

Hi

Give a try

| makeresults 
| eval col1=1, col2=1 
| append 
    [| makeresults 
    | eval col1=2, col2=2.1] 
| append 
    [| makeresults 
    | eval col1=3, col2=3] 
| eval compare = if(col1==col2,"true","false") 
| eval col1 = case(compare=="true",col1) 
| eval col2 = case(compare=="false",col2) 
| table col1,col2

View solution in original post

0 Karma

vnravikumar
Champion

Hi

Give a try

| makeresults 
| eval col1=1, col2=1 
| append 
    [| makeresults 
    | eval col1=2, col2=2.1] 
| append 
    [| makeresults 
    | eval col1=3, col2=3] 
| eval compare = if(col1==col2,"true","false") 
| eval col1 = case(compare=="true",col1) 
| eval col2 = case(compare=="false",col2) 
| table col1,col2
0 Karma

jiaqya
Builder

thanks, what is the entries are not on the same column.

any fix for that ?

0 Karma

vnravikumar
Champion

Hi

Try this

| makeresults 
| eval col1=1, col2=1 
| append 
    [| makeresults 
    | eval col1=2, col2=2.1] 
| append 
    [| makeresults 
    | eval col1=3, col2=3] 
| eval compare = if(col1==col2,"true","false") 
| eval test = if(compare=="true",col1,col2)
0 Karma

vnravikumar
Champion

New column test will have the result

0 Karma

jiaqya
Builder

Thank you Ravi, that works great..

0 Karma

jiaqya
Builder

Ravi, thanks, think spoke too soon, what if the value to compare is not on the same line .

ex: col1=1 and col2=2 on same line, but 2 is found on 2nd line in col1

| makeresults
| eval col1=1, col2=2
| append
[| makeresults
| eval col1=2, col2=1.1]
| append
[| makeresults
| eval col1=3, col2=3]
| eval compare = if(col1==col2,"true","false")
| eval test = if(compare=="true",col1,col2)

0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  🚀 Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...