Reporting

Searching a field using another field

ISP8055
Path Finder

Hi there,

I want to search events for example A =B*xy

Where B is another field name with different values depending on user input. * is the wildcard

So. I'm looking for events where A would be NYabxy, NYccxy, etc. Here value of B is NY

How would I do search syntax?
This doesn't work:
| search A=B*xy
as here B is considered a string not a field name.

would 
"where" be better alternative?

The main point of me doing this I wanna make my search more efficient as I want Splunk to only search events where A=NY*xy is applicable instead of searching every event with A field. 

Labels (1)

richgalloway
SplunkTrust
SplunkTrust

Yes, where is better than search in this case.  The search command accepts only strings and patterns on the RHS whereas the where command also accepts fields and expressions.  You could do something like this:

index=foo A=*
| where match(A,B.".*xy")

Here, the match function compares a field to a regular expression.  That regex is a concatention of field B, a wildcard (.*) and "xy".

Note that this does not save the indexers from reading all values of A, but it does filter A early so that helps some. 

---
If this reply helps you, Karma would be appreciated.

ISP8055
Path Finder

Hi there,

I applied this syntax. I'm getting the following error:
Error in 'where' command: The expression is malformed. Expected ).

richgalloway
SplunkTrust
SplunkTrust

Hmm...  Try this alternative

index=foo A=*
| where match(A,'B' . ".*xy")
---
If this reply helps you, Karma would be appreciated.
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!

Meet Splunk Observability Studio: AI-Assisted OpenTelemetry Instrumentation Without ...

Instrumentation is usually the last step or even an afterthought when building out a project. The feature ...

Federated Search for Cisco Security and Analytics Logging (SAL) is now GA on Splunk ...

Federated Search for Cisco  Security Analytics and Logging (SAL) is now generally available as part of the ...

Your Path to AgenticOps: AI Experiences for Every Splunk Practitioner

Your Path to AgenticOps: AI Experiences for Every Splunk Practitioner   Join us for a demo-driven look at how ...