Splunk Search

How Can I Create a Search To Compare 2 Fields with LIKE?

onur
Explorer

Hi Everyone,

I need to compare 2 fields with like command but I cant do it even if I tried many solutions.

For Example;

event1  field1="raceCar"  field2="car"
event2  field1="trying"  field2="hello"
event3  field1="splunk"  field2="helloSplunkEnterprise"

Desired result:

  event1  result=hit
  event2  result=miss
  event3  result=hit

 I tried | eval results= if (match()) but didnt work

Is there any suggestion about this SPL?

Thanks alot for your helps

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Technically, the problem you first described is solved - you have now described a different problem. You are trying to "join" events where the joining field values only partially match.

Given your example, can you extract just the string you want to match on?

For example, if you can assume that the id is made up of numbers, uppercase letters, minus and underscore, and sometimes preceded by an underscore, you could try something like this

| makeresults
| eval field1=split("536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656|remote_splunk_.com_536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656","|")
| mvexpand field1
| streamstats count as source
| eval name="datafrom".source
| eval {name}=random()%10 
| fields - name



| rex field=field1 max_match=0 "_?(?<id>[0-9A-Z_-]+)"
| eval id=mvindex(id,-1)
| stats values(*) as * by id

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| eval result=if(like(lower(field1),"%".lower(field2)."%") OR like(lower(field2),"%".lower(field1)."%"),"hit","miss")
0 Karma

onur
Explorer

Hi,

Thanks for your reply. When I tried this search, the problem is not solved. I am leaving 2 field below. These fields dont match.

Source1 = 536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656

Source2 = remote_splunk_.com_536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656

OR

Source1 = remote_splunk_.com_536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656

Source2 = 536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Here is a runanywhere example of the comparison working

| makeresults
| eval field1="536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656"
| eval field2="remote_splunk_.com_536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656"
| eval result=if(like(lower(field1),"%".lower(field2)."%") OR like(lower(field2),"%".lower(field1)."%"),"hit","miss")

If this is not your situation, you should expand on what exactly you are trying to do, perhaps with more realistic examples.

0 Karma

onur
Explorer

Actually, ı am trying to combine 2 searches. The first search is returning a search id in _audit index and the secone one is again returning a search id from _introspection index. In this way, I want to 2 combine these 2 searches and match the search id's. The main idea to create this search is that we want to find the total memory or cpu usage per ad hoc search. 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Technically, the problem you first described is solved - you have now described a different problem. You are trying to "join" events where the joining field values only partially match.

Given your example, can you extract just the string you want to match on?

For example, if you can assume that the id is made up of numbers, uppercase letters, minus and underscore, and sometimes preceded by an underscore, you could try something like this

| makeresults
| eval field1=split("536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656|remote_splunk_.com_536456786786_454AS6DASD-98SAD-787RET-98SAD-GASDA54656","|")
| mvexpand field1
| streamstats count as source
| eval name="datafrom".source
| eval {name}=random()%10 
| fields - name



| rex field=field1 max_match=0 "_?(?<id>[0-9A-Z_-]+)"
| eval id=mvindex(id,-1)
| stats values(*) as * by id
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...